我是一位能翻译文字的有用助手。
我正在编写一个脚本,尝试列出最新的以 .xls 结尾的文件。这看起来应该很容易,但是我遇到了一些错误。
代码:
for file in os.listdir('E:\\Downloads'):
if file.endswith(".xls"):
print "",file
newest = max(file , key = os.path.getctime)
print "Recently modified Docs",newest
错误:
Traceback (most recent call last):
File "C:\Python27\sele.py", line 49, in <module>
newest = max(file , key = os.path.getctime)
File "C:\Python27\lib\genericpath.py", line 72, in getctime
return os.stat(filename).st_ctime
WindowsError: [Error 2] The system cannot find the file specified: 'u'
Traceback (most recent call last): File "C:\Python27\sele.py", line 47, in <module> newest = max(files , key = os.path.getctime) File "C:\Python27\lib\genericpath.py", line 72, in getctime return os.stat(filename).st_ctime WindowsError: [Error 2] 系统找不到指定的文件: 'usage01.12.2015_31.12.2015(1).xls'
- user3580316getcttime
只在当前目录中查找,无法找到该文件。 - Martin KonecnyE:\Downloads\usage01.12.2015_31.12.2015(3).xls
。如何只获取文件名而不包括路径? - user3580316os.path.basename
。https://docs.python.org/2/library/os.path.html#os.path.basename - Martin Konecny