我有代码:
import os
import sys
fileList = os.listdir(sys.argv[1])
for file in fileList:
if os.path.isfile(file):
print "File >> " + os.path.abspath(file)
else:
print "Dir >> " + os.path.abspath(file)位于我的音乐文件夹("/home/tom/ music ")
当我使用以下命令调用它时:
python test.py "/tmp"我希望它能列出我的"/tmp“文件和文件夹的完整路径。但它打印的代码行如下:
Dir >> /home/tom/Music/seahorse-gw2jNn
Dir >> /home/tom/Music/FlashXX9kV847
Dir >> /home/tom/Music/pulse-DcIEoxW5h2gz这是正确的文件名,但路径错误(并且此文件也不在my Music文件夹中)..这段代码有什么问题?
发布于 2010-12-25 06:26:14
在检查文件是否存在并打印路径时,需要包括文件的完整路径:
dir = sys.argv[1]
fileList = os.listdir(dir)
for file in fileList:
file = os.path.join(dir, file) # Get the full path to the file.
# etc...https://stackoverflow.com/questions/4528531
复制相似问题