Learning Python - Have Question.

AlbaClause you at cogeco.ca
Sun Aug 27 15:40:46 EDT 2006


Tal Einat wrote:
 
> iapain wrote:
>> 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.

It turns out that I was using os.path.join() properly in the first place. 
And you're right, it is a very simple function.   Apparently, I was using
os.walk() improperly.  More specifically, I was not fully understanding how
os.walk() worked.   I've played with it some more in the Python interpreter
and I now have a very good understanding of how it works.

For example:

import os
d=os.walk('/server1')
for a, b, c in d:
        pass

This will place the path '/server1' in 'a', all directories in 'a' in a list
called 'b', and all files in a list called 'c'.  On the next iteration of
the loop, 'a' will contain the path '/server1' with the path of the first
directory in '/server1' appended.  So 'a' will be '/server1/directory1',
all of the directories in 'a' will be stored in 'b', and all of the files
in 'a' will be stored in 'c'.

So on the first iteration, a, b, and c would be:

a = server1
b = directory1, directory2, directory3
c = file1, file2, file3

On the second iteration of the loop, a, b, and c would be:

a = server1/directory1
b = subdirectory1, subdirectory2
c = d1file1, d1file2, d1file3

On the third iteration of the loop, a, b, and c would be:

a = server1/directory1/subdirectory1
b = []  #  there are no directories in subdirectory1 so b is empty.
c = sd1file1, sdfile2


>> 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.

Thank you for this.  The most daunting task in learning Python, is learning
all of the modules and functions that are available.  And there's a tonne
of them.  :-)

-- 
--
There are several things that I will never be:
  *  I will never be attracted to females.
  *  I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.




More information about the Python-list mailing list