import os
import time
torrent_folder = os.listdir(r'C:\users\chris\desktop\torrents')
for files in torrent_folder:
if files.endswith(".torrent"):
print(files + time.ctime(os.path.getatime(files)))
Run Code Online (Sandbox Code Playgroud)
我在运行此脚本时收到文件未找到错误.
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'TORRENT NAME.torrent
"
一切正常,直到time.ctime(os.path.getatime(files)
添加到混合中.
我希望脚本为文件夹中的每个文件显示"torrent name""last last modified".
为什么错误引用文件,按名称说它无法找到,我该如何解决?
您的files
变量只是文件名,而不是完整路径.因此,它将在您当前的工作目录中listdir
查找它,而不是在找到它的位置.
以下代码将使用完整路径名:
import os
import time
folder = r'C:\users\chris\desktop\torrent'
files = os.listdir(folder)
for file in files:
if file.endswith(".torrent"):
print(file + " " + time.ctime(os.path.getatime(os.path.join(folder,file))))
Run Code Online (Sandbox Code Playgroud)
该os.path.join()
联合收割机folder
,并file
给你一个完整的路径规范.例如,os.path.join("/temp","junk.txt")
会给你/temp/junk.txt
(在UNIX下).
然后它以与您尝试仅使用file
变量完全相同的方式使用它,获取最后的访问时间并以可读的方式对其进行格式化.