With the following python script ospath.py i reproduced error messages that trace backt to the amigapath.py module
import os import stat fileStats = os.stat ( 'Arch.gc' ) # a file in the current directory #if os.path.isdir ( fileStats ): # print 'Directory.' #if os.path.isfile ( fileStats ): # print 'File.' #if os.path.islink ( fileStats ): # print 'Shortcut.' if os.path.ismount ( fileStats ): print 'Mount point.'
The following results are given when uncommenting successively the 4 if blocks
11.Datas:Gui4Cli/GuiCreator> python:scripts/ospath.py Traceback (most recent call last): File "python:scripts/ospath.py", line 4, in <module> if os.path.isdir ( fileStats ): File "PYTHON:Lib/amigapath.py", line 204, in isdir st = os.stat(path) TypeError: coercing to Unicode: need string or buffer, amiga.stat_result found
11.Datas:Gui4Cli/GuiCreator> python:scripts/ospath.py Traceback (most recent call last): File "python:scripts/ospath.py", line 6, in <module> if os.path.isfile ( fileStats ): File "PYTHON:Lib/amigapath.py", line 217, in isfile st = os.stat(path) TypeError: coercing to Unicode: need string or buffer, amiga.stat_result found
11.Datas:Gui4Cli/GuiCreator> python:scripts/ospath.py Traceback (most recent call last): File "python:scripts/ospath.py", line 8, in <module> if os.path.islink ( fileStats ): File "PYTHON:Lib/amigapath.py", line 165, in islink st = os.lstat(path) TypeError: coercing to Unicode: need string or buffer, amiga.stat_result found
11.Datas:Gui4Cli/GuiCreator> python:scripts/ospath.py Traceback (most recent call last): File "python:scripts/ospath.py", line 10, in <module> if os.path.ismount ( fileStats ): File "PYTHON:Lib/amigapath.py", line 257, in ismount s1 = os.stat(path) TypeError: coercing to Unicode: need string or buffer, amiga.stat_result found
With the following python script ospath.py i reproduced error messages that trace backt to the amigapath.py module....
Did you start off with ...
Python-Docs-2.5/lib/module-stat.html 11.3 stat -- Interpreting stat() results "The stat module defines constants and functions for interpreting the results of os.stat(), os.fstat() and os.lstat() (if they exist). For complete details about the stat(), fstat() and lstat() calls, consult the documentation for your system. ..."
There's a sample script for 'walking a dir tree', but it's too technical for me. I don't know if even the file Stat'mode' is supported. I suspect it doesn't exist. Does it?
So you're passing the result of os.stat to os.path.isdir()
Quote:
os.path.isdir(path) Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.
Quote:
os.stat(path) Perform the equivalent of a stat() system call on the given path. (This function follows symlinks; to stat a symlink use lstat().)
The return value is an object whose attributes correspond to the members of the stat structure, namely:
st_mode - protection bits, st_ino - inode number, st_dev - device, st_nlink - number of hard links, st_uid - user id of owner, st_gid - group id of owner, st_size - size of file, in bytes, st_atime - time of most recent access, st_mtime - time of most recent content modification, st_ctime - platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows) ... ...
os.stat returns an amiga.stat_result object whereas os.path.isdir takes a path, ie a string / buffer.
Unless a stat result is supposed to convert to string automatically there is no bug in this context, rather you code is an incorrect usage of stat.
Checking gainst python on my linux box I get identical behaviour.
I would be tempted to say RTFM but that would be rude of me
As a newbee to python i just copied examples from a website. Looks like i was too confident here.
My mistake was due to the fact that i did not get an error message for the line in error, but that the msg pointed to a lib script. While this is probably not only a python problem, i am not accustomed to it. Well, i am reading more than i can digest obviously :(
>JosDuchit #1 fileStats = os.stat ( 'Arch.gc' ) # a file in the current directory The argument is wrong. It's 'os.stat(path)'. Try this at home:
#Amiga Python 53.29 import os st = os.stat( "Ram:") print st >>>> (16832, 0, 1822471436, 2, 0, 0, 0L, 1308634305, 1308634305, 1308634305, 1308634305, 1308634305, 1308634305, 512, 0L, 0)
The "STAT" module says -- 'All the variables below are simply symbolic indexes into the 10-tuple returned by os.stat(), os.fstat() or os.lstat().'
Well, it's a 16 tuple isn't it? I looked at help(os.stat_results) and the 'stavfs' module and can't determine what the tuple is saying.
>broadblues I would be tempted to say RTFM but that would be rude of me ... It sure would considering the depth of 2.5s Docs list. I read all the time, and it just too deep for one person to know all of it!
I found this: file:///PYTHON/Python-Docs-2.5.1/lib/os-file-dir.html "stat( path) Perform a stat() system call on the given path. The return value is an object whose attributes correspond to the members of the stat structure, namely: st_mode (protection bits), st_ino (inode number), st_dev (device), st_nlink (number of hard links), st_uid (user ID of owner), st_gid (group ID of owner), st_size (size of file, in bytes), st_atime (time of most recent access), st_mtime (time of most recent content modification), st_ctime (platform dependent; time of most recent metadata change on Unix, or the time of creation on Windows):"
10 elements given, the example in this doc does not match your tuple (16 items) For the Amiga all the times are the same: date of last change still i wonder what the last elements of the tuple are.
I am glad i'm not the only one having problems with the learning curve
I would be tempted to say RTFM but that would be rude of me ... It sure would considering the depth of 2.5s Docs list. I read all the time, and it just too deep for one person to know all of it!
Sure, but that's why I added a smiley
However, when I'm scripting, I find myslef googling every other function even though I'm relativley experienced in the languages I use (mostly php script wise ATM, but the principle holds across all languages). You don;t have to read the whole manual, but reading it little and often, saves more time than it takes to read it.