Learning Python - Have Question.

Tal Einat tal.no.no.spam at gmail.com
Sun Aug 27 03:10:08 EDT 2006


iapain wrote:
> > I'm just learning Python, and I have a question about os.path.join(dirpath,
> > name) and its use.  Simply put, I haven't figured out how to use it.
>
> First thing you have to remember while using python is "everything is
> an object". os.join.path concatenates one or more path for example
> os.path.join("c:", "myfolder") represent a path relative to current dir
> on c: drive.
>

Actually, os.path.join() is a simple function, there's nothing
Obejct-Oriented about it. "Everything is an object" simply means that
functions are objects, but that doesn't mean that the design of
everything is Object-Oriented.

> > Oh, I have one more question.   So far everything that I've played with
> > yields only the filename of file.  I am aware that os.walk will place the
> > pathnames and filenames in a tuple, but I'm wondering if there is a way to
> > input a full path directory from within Python.  Specifically, I want to be
> > able to get directories like one would get by entering "ls -l" at a *nix
> > shell prompt.
>
> you could easily do it with python. Its more than your expectation. The
> best would be to call os.system(shell cmd).

Using the shell for this is the least cross-platform solution possible.
Python's libraries have very corss-platform implementations of such
operations you should use them!

In this case, it seems you're looking for os.listdir(dir_name). Just
call it with the path of a directory and it will return a list of names
of all the files and directories therein. You can use os.path.isdir(),
os.path.isfile(), etc. to figure out what type of file system object
each item in the list is. You can check premissions for the current
user with os.access(). Finally, you can find out more data about any
file with the "stat" module, which is more low-level.

- Tal Einat
reduce(lambda m,x:[m[i]+s[-1] for i,s in enumerate(sorted(m))],
       [[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]




More information about the Python-list mailing list