python 复制替换文件
The python language provides a built-in module "shutil", which offers numerous high-level operations on files and collections of files. In particular, functions are provided to support the file copying and removal.
python语言提供了一个内置的模块“ shutil” ,该模块对文件和文件集合提供了许多高级操作。 特别是,提供了支持文件复制和删除的功能。
拷贝文件 (Copy file)
shutil.copyfile(src, dst, *, follow_symlinks=True)
Copies the contents of file from source(src) to destination(dst). src and dst are the path names as a string. dst must be the complete target file name.
将文件内容从源( src )复制到目标( dst )。 src和dst是字符串形式的路径名。 dst必须是完整的目标文件名。
Reminders,
提醒事项
If src and dst are the same locations, SameFileError is raised.
如果src和dst位于相同的位置, 则会引发SameFileError 。
The dst must be writable, else an IO Error will be raised.
dst必须可写,否则将引发IO错误 。
If dst already exists, it will be replaced.
如果dst已经存在,它将被替换。
If follow_symlinks are false and src is a symbolic link, then a new symbolic link will be created instead of copying the file src points to.
如果follow_symlinks为false并且src是符号链接,则将创建一个新的符号链接,而不是将src指向的文件复制到该链接。
使用shutil.copyfile()复制文件的Python代码 (Python code to copy the files using shutil.copyfile() )
# importing the modules
import os
import shutil
# getting the current working directory
src_dir = os.getcwd()
# printing current directory
print(src_dir)
# copying the files
shutil.copyfile('test.txt', 'test.txt.copy2') #copy src to dst
# printing the list of new files
print(os.listdir())
Output
输出量
/home/sradhakr/Desktop/my_work
'test.txt.copy2'
['python_samples', 'test.txt', 'test', 'test.txt.copy', 'test.txt.copy2']
移动文件 (Move files)
shutil.move(src, dst, copy_function=copy2)
The above method recursively moves the file from src to dst and returns the destination.
上面的方法将文件从src递归移动到dst并返回目标。
Reminders,
提醒事项
If the destination is an existing directory, then the src object is moved inside the given dst.
如果目标是现有目录,则将src对象移动到给定的dst中 。
In case the destination already exists and is not a directory, it will be overwritten using os.rename().
如果目标已经存在并且不是目录,则将使用os.rename()覆盖它。
In case the destination is on the current filesystem, then os.rename() is used. In the case of symlinks, a new symlink pointing to the target of src will be created in or as dst and src will be removed.
如果目标位于当前文件系统上,则使用os.rename() 。 对于符号链接 ,将在dst中或作为dst创建指向src目标的新符号链接 ,并删除src 。
The default copy_function is copy2(). Using copy() as the copy_function allows the move to succeed.
默认的copy_function是copy2() 。 使用copy()作为copy_function可以使移动成功。
使用Python代码移动文件 (Python code to move files)
Commnd to get the list of files and directories:
Commnd获取文件和目录列表:
-bash-4.2$ ls
python_samples test test.txt test.txt.copy test.txt.copy2
Code to move files:
移动文件的代码:
# Importing the modules
import os
import shutil
# getting src & dest directories
src_dir = os.getcwd() #gets the current working dir
dest_dir = src_dir + "/python_samples"
# move method to move the file
shutil.move('test.txt',dest_dir)
# the file 'test.txt' is moved from src to dest
print(os.listdir())
os.chdir(dest_dir)
# list of files in dest
print(os.listdir())
Output
输出量
'/home/sradhakr/Desktop/my_work/python_samples/test.txt'
['python_samples', 'test', 'test.txt.copy', 'test.txt.copy2']
['.git', '.gitignore', 'README.md', 'src', 'test.txt']
Reference: https://docs.python.org/3/faq/windows.html
参考: https : //docs.python.org/3/faq/windows.html
翻译自: https://www.includehelp.com/python/copy-and-replace-files-in-python.aspx
python 复制替换文件