使用 Python 重命名和移动文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42541748/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rename and move file with Python
提问by Zipper1365
I have python script that compares existing file names in a folder to a reference table and then determines if it needs to be renamed or not.
我有 python 脚本,它将文件夹中的现有文件名与引用表进行比较,然后确定是否需要重命名。
As it loops through each filename:
当它遍历每个文件名时:
'oldname' = the current file name
'newname' = what it needs to be renamed to
I want rename the file and move it to a new folder "..\renamedfiles"
我想重命名文件并将其移动到一个新文件夹“..\renamedfiles”
Can I do the rename and the move at the same time as it iterates through the loop?
我可以在遍历循环的同时进行重命名和移动吗?
Update: Apologies, I'm fairly green with scrpting in general but this appears to be pretty basic. shutil.moveis exactly what I needed THANKS- I just didn't know to look for it. Successful test below. Now to work it into the script.
更新:抱歉,一般来说我对 scrpting 还很陌生,但这似乎是非常基本的。shutil.move正是我需要的。谢谢-我只是不知道要寻找它。下面测试成功。现在将其处理到脚本中。
import shutil
os.chdir('C:\Users\me\Desktop\New folder')
renFolder= 'Renamed'
oldname = 'Test.txt'
newname= 'renTest.txt'
shutil.move(oldname, renFolder+'/'+newname)
采纳答案by ShadowRanger
os.rename
(and os.replace
) won't work if the source and target locations are on different partitions/drives/devices. If that's the case, you need to use shutil.move
, which will use atomic renaming if possible, and fallback to copy-then-delete if the destination is not on the same file system. It's perfectly happy to both move and rename in the same operation; the operation is the same regardless.
os.rename
os.replace
如果源位置和目标位置位于不同的分区/驱动器/设备上,(和)将不起作用。如果是这种情况,您需要使用shutil.move
,如果可能,它将使用原子重命名,如果目标不在同一文件系统上,则回退到复制然后删除。在同一个操作中移动和重命名是非常愉快的;不管怎样,操作都是一样的。
回答by Danny
Yes you can do this. In python you can use the movefunction in shutillibrary to achieve this.
是的,你可以这样做。在python中,您可以使用shutil库中的move函数来实现这一点。
Lets say on linux, you have a file in /home/user/Downloads folder named "test.txt" and you want to move it to /home/user/Documents and also change the name to "useful_name.txt". You can do both things in the same line of code:
假设在 linux 上,您在 /home/user/Downloads 文件夹中有一个名为“test.txt”的文件,您想将其移动到 /home/user/Documents 并将名称更改为“useful_name.txt”。你可以在同一行代码中做这两件事:
import shutil
shutil.move('/home/user/Downloads/test.txt', '/home/user/Documents/useful_name.txt')
In your case you can do this:
在你的情况下,你可以这样做:
import shutil
shutil.move('oldname', 'renamedfiles/newname')
Cheers.
干杯。
回答by Ofer Arial
To do both of the operations, you can use the os.rename(src, dest)
function.
要执行这两个操作,您可以使用该os.rename(src, dest)
函数。
You should have the wanted directory to save the file in, and the new file name. You can do this for every file you run across in your loop.
您应该拥有保存文件的所需目录和新文件名。您可以对循环中遇到的每个文件执行此操作。
For example:
例如:
# In Windows
dest_dir = "tmp\2"
new_name = "bar.txt"
current_file_name = "tmp\1\foo.txt"
os.rename(current_file_name, os.path.join(dest_dir, new_name))
The rename
function allows you to change the name of the file and it's folder at the same time.
该rename
功能允许您同时更改文件名和文件夹名。
To prevent any errors in renaming and moving of the file, use shutil.move.
为防止在重命名和移动文件时出现任何错误,请使用shutil.move。
回答by Mathieu Bour
Create a Python file in your desired directory and write something like that :
在您想要的目录中创建一个 Python 文件并编写如下内容:
import os
for filename in os.listdir("."):
if(filename ...):
newFilename = ...
os.rename(filename, newFilename)