Python file copy

Python File Copy Method

How to copy file in python

We have  efficient methods for python file copy  ,directory and entire folder copying using shutil module.

We know that copying a file ,deleting a file, directory and compressing a file are a boring stuff , the python automate these stuff with the help of Shell utility module(shutil).

The syntax of copy method s as follows:

shutil.copy(source,destination)

The Following code snippet copy a file ,folder and directory .

 

      import os,shutil

      shutill.copy(‘/Users/apple/test.txt’ ,’/Users/apple/newfile/)

The above code copy the file test.txt to the newfile folder of apple macbook directory.

        

Copying the Entre Directory

Let us consider a situation ,we need to copy the entire contents of a folder , in that case ,we need to use the copytree() method.

import os,shutil

      shutill.copytree(‘/Users/apple/test’ ,’/Users/apple/newfile/’)

The copytree () works exactly same as copy method but only difference is it copies all the files. 

Removing a file ,folder in python

The os module provides three different methods to remove a file,folder and directory : which are discussed below.

os.unlink(path) : It will delete the file permanently in the path specified .

os.rmdir(path) : This method removes all the files in the directory.

os.rmtree(path): This removes all the folders, subfolders and files present in the file.

we Should be very careful when using the file delete methods , once the file is deleted it cannot be restored. 

To solve this problem , python come with a module called send2trash , we need to install this module ,once installed we can use send2trash() method to delete the file into the trash.

Safe Deleting with send2trash Module

As explained in the previous paragraph , it is used to delete the files to the trash instead permanently.

install this module using pip command.

The following code illustrate this :

import send2trash

send2trash.send2trash(test.txt)

The above code send the file test.txt to the trash . If the file is not present , create a file using open method().

The delete methods are very useful like listing the files in a directory and checking for existence of a file.

Directory Walk

Lets say , you want to visit every file and folder in a directory , python provides walk() method of os module to this job.

Consider the following example:

 import os

 for folderName, subfolders, filenames in os.walk(‘/Users/apple/test/’):

 print(‘The current folder is ‘ + folderNam)

 for subfolder in subfolders:

 print(‘SUBFOLDER OF ‘ + folderName + ‘: ‘ + subfolder)

 for filename in filenames:

 print(‘FILE INSIDE ‘ + folderName + ‘: ‘+ filename)

 print(”) 

The above list all the files in the directory test in the form folders ,subfolders and files.

Compressing the Files in python

Python provides facility to compress and extract the file using zipfile module.

We already know the compressing a file , the reason to compress the file is save the file , once you performed the compress operation , all files will be converted into a single file called archive file.

In python we can perform compress and extraction of files in efficient manner. The following code snippets illustrate this:

  

Reading a zip file

import os

examplezip = zipfile.ZipFile(‘test.zip’)

examplezip.namelist()

testinfo = examplezip.getinfo(test.txt)

testinfo.filesize

testinfo.compress_size

 

 

Before using the zip module , we need to create a zip object to perform the operation . In the above code , first we import the os module ,

then we created a zip object called examplezip, and the namelist() method lists all the files in the zip file.

we can get the information about single file in zip file using getinfo () method of zip object. similarly the file size() method return the size of the original file and compress_size will return the compressed file size. 

Extracting the files

we can extract the files from a archive file using extractall() and extract method() , If we want to extract only a single file then use the extract() otherwise use extractall() to retrieve all files. the code is as follows:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

examplezip.extractall()

examplezip.close()

examplezip is the zip object created above and it extract all the files in the current working directory . Once all the operations are over then close() the zipobject. 

Adding a file to zip object

We use the write method of zipobject to add the files to the zip object , we need to specify the algorithm for zipping the file otherwise the default algorithm will be used. The following code illustattethis:

 import zipfile
 newZip = zipfile.ZipFile('new.zip', 'w')
 newZip.write('test.txt', compress_type=zipfile.ZIP_DEFLATED) >>> newZip.close()

This code will create a zip file called new.zip , we use the  ‘w’ to indicate adding the files for compression .next we call the write method by sending the file name to be compressed and algorithm for compreesing.

Final word

I discussed briefly the python methods for basic operation like copying,deleting and compressing ,I will add more content in future .