How to copy related files in Python File Replication

Source: Internet
Author: User

When copying related files using Python files, we can use the shutil module to copy them. If you are confused about copying Python files, you can use the following methods to learn about the actual operations of Python file replication.

Copy and rename Python files when we want to copy files, we can use the shutil module:

 
 
  1. import shutil  
  2. shutil.copy(myfile, tmpfile)  

Last access time and last modification time of the copy:

 
 
  1. shutil.copy2(myfile, tmpfile) 

Copy a directory tree:

 
 
  1. shutil.copytree(root_of_tree, destination_dir, True) 

The third parameter of the Copytree specifies the processing of symbolic links. True indicates that the symbolic link is retained; False indicates that the physical copy of the Python file replaces the symbolic link. The Python language supports the cross-platform structure of path names: OS. path. the correct delimiter (used in UNIX and Mac OS x operating systems, and in Windows) can be used to join directories and file names. The variable OS. curdir and OS. pardir indicates the current working directory and its parent directory respectively. Like the following UNIX operating system commands

 
 
  1. cp ../../f1.c . 

You can use the Python language to provide a cross-platform implementation:

 
 
  1. shutil.copy(os.path.join(os.pardir,os.pardir,’f1.c’), os.curdir) 

The rename function in the OS module is usually used to rename a file:

 
 
  1. os.rename(myfile, ’tmp.1’)  

Rename myfile to '. This function can also be used to move files in the same file system. Here, we move myfile to Directory d

 
 
  1. os.rename(myfile, os.path.join(d, myfile)) 

When moving files across file systems, you can use shutil. copy2 to copy Python files and then delete the original copy, as shown below:

 
 
  1. shutil.copy2(myfile, os.path.join(d, myfile))  
  2. os.remove(myfile)  

The method for moving files later is the safest.

 
 
  1. copymode(sor,sten) 

The above content introduces the practical application solution of Python file replication.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.