(新人求教)python总是报错,提示找不到明明存在的文件
我的代码如下,如果把test_path.py这个程序放在c:\path这个被读取的路径下就正常,但如果放到别的任何路径下去运行总是提示错误,说找不到某个文件,而那个文件确实是存在的。也就是说,只要在当前文件夹下本程序才正常。
# 文件名:test_path.py
# -*- coding: utf-8 -*-
import os
path = 'c:\\path'
f_list = os.listdir(path) # 这一行代码可正常列出所有文件。
print f_list
for x in f_list:
size = os.path.getsize(x)
print '%s size: %s KB' % (x, size/1024)
下面是输出的信息
['7-zip.chm', '7-zip.dll', '7-zip32.dll', '7z.dll', '7z.exe', '7z.sfx', '7zCon.sfx', '7zFM.exe', '7zG.exe', 'app', 'descript.ion', 'History.txt', 'License.txt', 'readme.txt', 'Test', 'Uninstall.exe']
Traceback (most recent call last):
File "C:\Python\test_path.py", line 9, in <module>
size = os.path.getsize(x)
File "C:\Python\Python27\lib\genericpath.py", line 57, in getsize
return os.stat(filename).st_size
WindowsError: [Error 2] : '7-zip.chm'
[Finished in 0.2s with exit code 1]
[shell_cmd: python -u "C:\Python\test_path.py"]