简体   繁体   English

shutil.move删除文件扩展名

[英]shutil.move removing file extensions

You guys have been a great help with my learning python. 你们对我学习python起到了很大的帮助。 I was tasked today with modifying an old employees code and have since downloaded several resources to help. 今天,我受命修改旧员工代码,此后下载了一些资源来提供帮助。 I am renaming files in a folder. 我正在重命名文件夹中的文件。 Basically a user inputs something in a text box and that is being added on to the end of the file. 基本上,用户在文本框中输入内容,然后将其添加到文件末尾。

if len(self.toLoc.get()) == 0:
        searchRev = "_R" + newRev 
        for filename in os.listdir(App.pdfDir):
            sep = searchesri
            rest = filename.split(sep, 1)[0] + searchRev + fromLocation
            shutil.move(os.path.join(App.pdfDir, filename), os.path.join(App.pdfDir, rest))

it works perfectly, the only issue i have is that it does not keep the file extensions and unfortunatly there are pdfs and xlsx's in the dir so I can't just hard code pdf on the end. 它可以完美地工作,我唯一的问题是它不保留文件扩展名,并且不幸的是在目录中有pdf和xlsx,所以我不能只对pdf进行硬编码。 I tried to see if the os function had a way of getting it but i kind of confused myself... 我试图看看os函数是否有办法获取它,但是我有点困惑……

I'm not sure what searchesri is, but you're throwing away part of the file name in filename.split(sep, 1)[0] . 我不确定什么是searchesri ,但是您正在丢弃filename.split(sep, 1)[0]部分文件名。 Maybe you need to append filename.split(sep, 1)[1] to the end of your destination file name? 也许您需要将filename.split(sep, 1)[1]附加到目标文件名的末尾?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM