相关疑难解决方法(0)

Python:在目录中查找扩展名为.MP3的最新文件

我试图在Python中找到最近修改过的(从此处开始'最新')特定类型的文件.我现在可以获得最新版本,但无论哪种类型都无关紧要.我想只获得最新的MP3文件.

目前我有:

import os

newest = max(os.listdir('.'), key = os.path.getctime)
print newest
Run Code Online (Sandbox Code Playgroud)

有没有办法修改它只给我最新的MP3文件?

python file-io

56
推荐指数
2
解决办法
4万
查看次数

目录中的Python最新文件

我正在编写一个脚本,我试图列出以.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
Run Code Online (Sandbox Code Playgroud)

错误:

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'
Run Code Online (Sandbox Code Playgroud)

python directory file listdir

2
推荐指数
1
解决办法
6453
查看次数

标签 统计

python ×2

directory ×1

file ×1

file-io ×1

listdir ×1