[英]How to save the renamed file using os python
我正在生成網頁的多個 pdf,因此每次生成 pdf 文件時,我都想重命名它並保存該特定文件。 但是我的代碼在生成文件時重命名了文件,但刪除了較早的文件。 下面是我的腳本:
os.chdir(new_path)
files = filter(os.path.isfile, os.listdir(new_path))
files = [os.path.join(new_path, f) for f in files] # add path to each file
files.sort(key=lambda x: os.path.getmtime(x))
newest_file = files[-1]
# os.path.join(new_path,search + str(photoid) + str(seaid_destination) + ".pdf",newest_file)
os.rename(newest_file, search + str(photoid) + str(seaid_destination) + ".pdf")
它正確地重命名了它,但它刪除了早期的文件,以便如何保存這個重命名的文件。 我試過os.path.join
但文件沒有保存。 要保存的文件是newest_file
最初生成的文件是Google.pdf
所以我想將其重命名為1.pdf
並將此文件保存在特定文件夾中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.