這是一個複雜的問題...
當我運行Python時,我想確定臨時文件夾中的最新Excel文件(%USERPROFILE%\\AppData\\Local\\Temp\\
)並將其複製到Windows上的D
驅動器。如何檢查並獲取臨時文件夾中的最新文件(.xlsx)?
我搜索的例子,但沒有發現任何...
我怎樣才能解決這個問題?
這是下面的代碼...
import os
import win32com.client as win32
import time
import subprocess
import tempfile
dated_files = [(os.path.getmtime(fn), os.path.basename(fn)) for fn in os.listdir(os.getenv('TEMP')) if fn.lower().endswith('.xlsx')]
dated_files.sort()
dated_files.reverse()
newest = dated_files[0][1]
print(newest)
=============================== ==============================
Traceback (most recent call last):
File "D:/1002.py", line 11, in <module>
for fn in os.listdir(os.getenv('TEMP')) if fn.lower().endswith('.xlsx')]
File "D:/M1002.py", line 11, in <listcomp>
for fn in os.listdir(os.getenv('TEMP')) if fn.lower().endswith('.xlsx')]
File "C:\Python35\lib\genericpath.py", line 55, in getmtime
return os.stat(filename).st_mtime
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'SampleCheck.xlsx'
Process finished with exit code 1
你有沒有試圖自己實現呢?你能請你提供一個你最近嘗試的[mcve]嗎? – idjaw
@idjaw 當然我自己嘗試了3天.. 但是沒有任何想法... – everline
請顯示您的代碼。確保您符合提供[mcve] – idjaw