我在列表中存储了200个名称。我从一个网站上下载了200个pdf文件,但名称却毫无意义且丑陋。所以我想将所有200个pdf文件重命名为200个名称。 pdf的创建时间可以与名称列表匹配,这意味着最早的pdf应该重命名为名称列表中的第一个元素(这里是1)。你知道如何解决吗?谢谢
# this is my file name in a text
file_name = list(range(1,201))
print (file_name)
with open('name.txt', 'w') as f:
for item in my_list:
f.write("%s\n" % item)
# this is the pdf files I want to change their name, here I order them by their creating time
import glob
import os
files = glob.glob("*.pdf")
files.sort(key=os.path.getctime)
print("\n".join(files))
据我了解,您想使用列表中存储的200个名称来重命名pdf文件(以asc时间排序)。
这是我的方法:
浏览所有pdf文件,然后将它们重命名为列表中存储的值。
import glob
import os
file_name = list(range(1,201))
files = glob.glob("*.pdf")
sorted_pdf_files = files.sort(key=os.path.getctime)
for file in sorted_pdf_files:
os.rename(file, counter+'.pdf')
counter+=1