This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2012年06月02日 13:06 by javahaxxor, last changed 2022年04月11日 14:57 by admin. This issue is now closed.
| Messages (3) | |||
|---|---|---|---|
| msg162133 - (view) | Author: Adrian Bastholm (javahaxxor) | Date: 2012年06月02日 13:06 | |
os.path.isfile doesn't reckognize a .picasa.ini file as a file
and os.path.isdir doesn't reckognize a directory as a directory
code:
def traverse (targetDir):
currentDir = targetDir
dirs = os.listdir(targetDir)
# dirs = [x for x in os.listdir('.') if x.endswith('.JPG')]
for entry in dirs:
if os.path.isdir(entry):
print("Traversing " + entry)
traverse(entry)
else:
print("Not dir: " + entry)
if os.path.isfile(entry):
print("Processing " + " " + currentDir + " " + entry)
else:
print("Not file: " + entry)
print("\n")
return True
The test directory contains jpg files and a folder with some other jpgs and a subfolder containing jpgs
|
|||
| msg162146 - (view) | Author: Hynek Schlawack (hynek) * (Python committer) | Date: 2012年06月02日 16:01 | |
I think your problem is a different one: os.listdir() doesn't return full paths and os.path.isfile()/isdir() return False if the supplied path doesn't exist.
For example if you have this directory structure:
foo/
foo/bar/
foo/bar/baz
Calling os.listdir('foo') will return ["bar"]. Calling os.isdir('bar') will return False because it can't find the file.
Have a look at os.walk() which was written with your use case in mind.
|
|||
| msg162161 - (view) | Author: Adrian Bastholm (javahaxxor) | Date: 2012年06月02日 18:13 | |
You're right, my code was shite. Strange though it seemed to work on some files. The following updated version does everything as intended with the help of os.path.join:
def traverse (targetDir):
currentDir = targetDir
dirs = os.listdir(targetDir)
for entry in dirs:
if os.path.isdir(os.path.join(currentDir,entry)):
print("Traversing " + os.path.join(targetDir,entry))
traverse(os.path.join(targetDir,entry))
else:
if os.path.isfile(os.path.join(targetDir,entry)):
print("Processing" + " " + os.path.join(currentDir,entry))
else:
print("Not file: " + entry)
print("\n")
|
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:57:31 | admin | set | github: 59190 |
| 2012年06月02日 18:13:28 | javahaxxor | set | messages: + msg162161 |
| 2012年06月02日 16:01:08 | hynek | set | status: open -> closed nosy: + hynek messages: + msg162146 resolution: not a bug stage: resolved |
| 2012年06月02日 13:06:52 | javahaxxor | create | |