使用 Python 重命名和移动文件
Rename and move file with Python
我有一个 Python 脚本,可以将文件夹中的现有文件名与引用 table 进行比较,然后确定是否需要重命名。
当它循环遍历每个文件名时:
'oldname' = the current file name
'newname' = what it needs to be renamed to
我想重命名文件并将其移动到新文件夹“..\renamedfiles”
我可以在遍历循环时同时进行重命名和移动吗?
在您想要的目录中创建一个 Python 文件并写入类似的内容:
import os
for filename in os.listdir("."):
if(filename ...):
newFilename = ...
os.rename(filename, newFilename)
要执行这两个操作,您可以使用 os.rename(src, dest)
函数。
您应该有保存文件的所需目录和新文件名。您可以对循环中 运行 遇到的每个文件执行此操作。
例如:
# 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))
rename
功能允许您同时更改文件名及其文件夹。
为防止重命名和移动文件时出现任何错误,请使用 shutil.move。
如果源位置和目标位置位于不同的 partitions/drives/devices,os.rename
(和 os.replace
)将不起作用。如果是这种情况,您需要 use shutil.move
,这将尽可能使用原子重命名,如果目标不在同一文件系统上,则回退到复制然后删除。在同一个操作中移动和重命名是非常高兴的;不管怎样,操作都是一样的。
是的,你可以做到。在 Python 中,您可以使用 shutil 库中的 move
函数来实现此目的。
假设 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')
在你的情况下你可以这样做:
import shutil
shutil.move('oldname', 'renamedfiles/newname')
自 Python 3.4 起,使用 pathlib
. Moving/renaming a file is done with rename
or replace
(will unconditionally do the replace). So combining with the parent
attribute and the concat operator 可以轻松完成路径操作,您可以:
from pathlib import Path
source = Path("path/to/file/oldname")
target = source.replace(source.parent / "renames" / "newname")
我有一个 Python 脚本,可以将文件夹中的现有文件名与引用 table 进行比较,然后确定是否需要重命名。
当它循环遍历每个文件名时:
'oldname' = the current file name
'newname' = what it needs to be renamed to
我想重命名文件并将其移动到新文件夹“..\renamedfiles”
我可以在遍历循环时同时进行重命名和移动吗?
在您想要的目录中创建一个 Python 文件并写入类似的内容:
import os
for filename in os.listdir("."):
if(filename ...):
newFilename = ...
os.rename(filename, newFilename)
要执行这两个操作,您可以使用 os.rename(src, dest)
函数。
您应该有保存文件的所需目录和新文件名。您可以对循环中 运行 遇到的每个文件执行此操作。
例如:
# 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))
rename
功能允许您同时更改文件名及其文件夹。
为防止重命名和移动文件时出现任何错误,请使用 shutil.move。
os.rename
(和 os.replace
)将不起作用。如果是这种情况,您需要 use shutil.move
,这将尽可能使用原子重命名,如果目标不在同一文件系统上,则回退到复制然后删除。在同一个操作中移动和重命名是非常高兴的;不管怎样,操作都是一样的。
是的,你可以做到。在 Python 中,您可以使用 shutil 库中的 move
函数来实现此目的。
假设 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')
在你的情况下你可以这样做:
import shutil
shutil.move('oldname', 'renamedfiles/newname')
自 Python 3.4 起,使用 pathlib
. Moving/renaming a file is done with rename
or replace
(will unconditionally do the replace). So combining with the parent
attribute and the concat operator 可以轻松完成路径操作,您可以:
from pathlib import Path
source = Path("path/to/file/oldname")
target = source.replace(source.parent / "renames" / "newname")