stupid question about os.listdir
Jason Kratz
eat at joes.com
Thu Jun 26 22:57:33 EDT 2003
Here is more clarification. Here is my script called backup.py
import os.path
import os
def getdirs(path):
dirs = []
os.chdir(path)
for entry in os.listdir(path):
if os.path.isdir(entry):
dirs.append(entry)
return dirs
print getdirs('/')
if I run this from the command line on linux with 'python backup.py' it
works *if* I have os.chdir in there. if I comment it out it doesnt list
starting from the root dir...it starts in my home dir.
If go into the interactive command mode by just typing 'python' at the
prompt and do:
import os
os.listdir('/') then it prints out the dirs under root.
incidentally this happens on windows as well
Jason Kratz wrote:
> OK. I've search on google groups and around the web for this and I
> haven't found an answer. I'm a Python newbie and have what I assume is
> a basic question. os.listdir takes a pathname as an arg but it doesn't
> actually list the contents of the dir I pass in. it always operates on
> the current dir (wherever the script is run) and I have to chdir
> beforehand. Is that how its supposed to work? If so what is the point
> in passing in a pathname?
>
> thanks,
>
> Jason
>
More information about the Python-list
mailing list