• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

Sometimes you need to do more with files than just reading and writing data. You might need to find out the information about the file like whether it exists, what is the path of the file is, whether the path is a directory or a file, etc. So, to get all these information Python provides path related utilities.







In this tutorial we will learn

Various OS operations in Python

Step 1: You can identify the name of the O.S system by calling through Python code. In our case, it will print "nt" as we are using window O.S

Python OS Module, Shell Script Commands

When you run the code you can observe that you are printing out the letters "nt" as such we are using Windows eight and the OS name as far as programmer are concerned it is denoted as nt. Now you might encounter different name based on what kind of computer you are using, if you are using Linux or Mac, it might print different name.

Step 2: In this step we can see, how we can use Python O.S path utilities to check information about our text file that we created like whether it exist or not, and even if it exists, is it a file or directory etc. Make sure that you import O.S path before executing this code.

Python OS Module, Shell Script Commands

So when you run the code, it will check the file format and return the result in true or false. In our case, it is a file, so in output it will show true for the file and false for the directory.

Step 3: With Python code you can also track the path of the txt.file (guru99.txt) and can split the name of the file and name.

  1. Print the path of the file where text file "guru99.txt" is created
  2. Split the path of the file and name of the text file

Python OS Module, Shell Script Commands

Step 4: You can fetch the information about the text file last modified

Python OS Module, Shell Script Commands

  • Code Line#15- It tells the day, date, month, year and time when .txt file (guru99) was last modified. We use the path module to get the file modification time details, and then we are going to use the time classes c time function to convert that into a readable time. So when we run the code, we can see the file guru99.txt was last modified on Thu, June 11th at 16:07 2015.
  • Code Line#17- It does the same thing giving information about file modification, but it has a different format to represent it. Here we use Get Modification Time function (path.getmtime("guru99.txt")). Now instead of using the c time function we going to use From Time Stamp function and going to construct a date time object. In output, you can see file modification time detail is printed out in different format 2015-06-11, 16:07:47.1816

Here is the complete code

import os
from os import path
import datetime
from datetime import date, time, timedelta
import time
  def main():
	# Print the name of the OS
		  print os.name
    # Check for item existence and type
 		 print "Item exists: " + str(path.exists("guru99.txt"))
		 print "Item is a file: " + str(path.isfile("guru99.txt"))
	     print "Item is a directory: " + str(path.isdir("guru99.txt"))
   # Work with file paths
	  print "Item's path: " + str(path.realpath("guru99.txt"))
	  print "Item's path and name: " + str(path.split(path.realpath("guru99.txt")))
   #Get the modification time
	  t = time.ctime(path.getmtime("guru99.txt"))
	  print t
    print datetime.datetime.fromtimestamp(path.getmtime("guru99.txt"))
if __name__== "__main__":
	  main()

Python File System Shell Methods

Python provides the functions for manipulating files by using the Operating System Shell utilities.

Let's study them-

Step 1: In this step we are going to see how we can get the path to the file in the current directory and print out file name and path separately.

  1. Declaring variable
  2. Applying split function on variable

Python OS Module, Shell Script Commands

  • First we are going to check that our "guru99.txt" file exists or not. Since we have created guru99.txt file earlier, we know it exists, and we will carry on further with the code
  • We store the file path in the variable "src" if your file exist
  • Once we get the path, we going to separate the path and the file name
  • For that, we are going to use the split "path.split(src)" function on source variable
  • Code when executed prints out "file name" and "file path" separately

Step 2: We can use Shutil Module to create a copy of the existing file, setting permission, etc. Here we used to create a copy of our existing file "guru99.txt."

Python OS Module, Shell Script Commands

  • Take the original file name "guru99.txt" and add letters .bak at the end "guru99.txt.bak", this name with .bak extension is going to be our duplicate copy
  • And then we going to use utility's copy function to copy from source to the destination
  • When you run the code, you will see a duplicate file with .bak extension is created on right-hand side of the panel

Step 3: Copy function only copies the content of the file but no other information, to copy other things like meta-data associated with the file, file permission and other information you have to use "copystat" function. Before we run this code, we have to delete our copy file "guru99.text.bak".

Python OS Module, Shell Script Commands

Once you deleted the file and run the program it will create a copy of your .txt file but this time with all the information like file permission, modification time or meta-data information. You can go to your O.S shell to verify the information.

Python OS Module, Shell Script Commands

Step 4: You can rename the original file, we have changed the file name from "Guru99.txt" to "Career.guru99.txt."

Python OS Module, Shell Script Commands

  • To rename "guru99.txt" file, we going to use "rename function" in the OS module
  • So when the code is executed, you can observe that a new file "career.guru99.txt" is created on the right side of the panel, which we renamed for our original file.

Step 5: You can also create an archive file from Python but make sure you have your import statement correct and in order. Here the import statement for the archive is "from shutil import make_archive."

Python OS Module, Shell Script Commands

  • The archive file for "guru99" will show all the content of the directory
  • Import make_archive class from module shutil
  • Use the split function to split out the directory and the file name from the path to the location of the text file (guru99)
  • Then we call the module "shutil.make_archive("guru99 archive, "zip", root_dir)" to create archive file, which will be in zip format
  • After then we pass in the root directory of things we want to be zipped up. So everything in the directory will be zipped
  • When you run the code, you can see the archive zip file is created on the right side of the panel.
  • Once your archive file is made, you can right-click on the file and select the O.S, and it will show your archive files in it as shown below

Python OS Module, Shell Script Commands

Now your archive.zip file will appear on your O.S (Windows Explorer)

Python OS Module, Shell Script Commands

When you double-click on the file, you will see the list all the files in there.

Python OS Module, Shell Script Commands

Step 6: In Python we can have more control over archive since we can define which specific file to include under archive. In our case, we have included two files under archive "guru99.txt" and "guru99.txt.bak".

Note: Here we don't give any command to "close" the file like "newzip.close" because we use "With" scope lock, so when program falls outside of this scope the file will be cleaned up and is closed automatically.

Python OS Module, Shell Script Commands

  • Import Zipfile class from zip file Python module. This module gives full control over creating zip files
  • We create a new Zipfile with name ( "testguru99.zip, "w")
  • Creating a new Zipfile class, requires to pass in permission because it's a file, so you need to write information into the file as newzip
  • We used variable "newzip" to refer to the zip file we created
  • Using the write function on the "newzip" variable, we add the files "guru99.txt" and "guru99.txt.bak" to the archive

When you execute the code you can see the file is created on the right side of the panel with name "guru99.zip" and then click on your zip folder file, it will show both files in the zip folder

When you -> right click on file (testguru99.zip) and -> select your O.S (Windows Explorer), it will show the archive files in the folder as shown below.

Python OS Module, Shell Script Commands

When you double click on file "testguru99.zip", it will open another window, and this will show the files included in it.

Python OS Module, Shell Script Commands

Here is the complete code

import os
import shutil
from zipfile import ZipFile
from os import path
from shutil import make_archive

def main():
# make a duplicate of an existing file
	if path.exists("guru99.txt"):
# get the path to the file in the current directory
	src = path.realpath("guru99.txt");
# rename the original file
	os.rename("career.guru99.txt","guru99.txt")
# now put things into a ZIP archive
	root_dir,tail = path.split(src)
    shutil.make_archive("guru99 archive", "zip", root_dir)
# more fine-grained control over ZIP files
	with ZipFile("testguru99.zip","w") as newzip:
	newzip.write("guru99.txt")
	    newzip.write("guru99.txt.bak")
if __name__== "__main__":
	  main()

Summary:

  • Python allows you to extract information related to file like path of the file, format of a file (directory or a file), etc. through O.S path utilities.
  • Date, Time and timedelta classes help you perform time functions in Python
  • You Verify the file format (file or directory) and its existence using str(path.exists/isfile/isdir(file.txt))
  • You can Track the path of the file Str(path.split(path.realpath("textfile")))
  • Use code datetime.datetime.fromtimestamp(path.getmtime("textfile") to get modify date of a file
  • To create a copy of the existing file by use code shutil.copy (src,dst)
  • To copy all the information of original file to duplicate file like file permission, modification time or meta-data information by use code shutil.copystat(src,dst)
  • To create a zip file in root directory by use code "shutil.make_archive("name","zip", root_dir)

 

YOU MIGHT LIKE: