Simple Pycodes

Simple Pycodes

Python provides a good amount of support when it comes to operating system tasks on file and folder management. beside the logic of the language itself which makes it easier  to solve long and boring tasks the other advantage of using python for OS tasks is that we can use on UNIX / LINUX, Windows and Mac operating systems besdie very few exeptions, so in this short tutorial we are going to explore OS module mostly and a bit of Glob and shutil modules.

so in this short article we are going to take a look at 10 most or almost useful file operation commands which we might need to use in python codes.

Os module is a standard library and its ready to use by using import os.

acording to official documentation OS module provides a portable way of using operating system dependent functionality. and this is mostly the case of use in many scripts.

glob  module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell.

shutil module similar to os and shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal. For operations on individual files, see also the os module.

Before trying os commands on ipython make sure that you already import the modules, this can be done by import os, glob, shutil so from now i assume you already imported the modules and there is no need to mention it in our examples.

--------------------------------------------------------------------------------------------------------------

1) Make  a folder

In python folders refered as directories

os.mkdir("directory / folder name") the name should pass as string.

--------------------------------------------------------------------------------------------------------------

2) Move to folder

Current folder = os.getcwd()

os.chdir(destination folder)

--------------------------------------------------------------------------------------------------------------

3) Rename a file or folder

os.rename(oldname, newname)

--------------------------------------------------------------------------------------------------------------

4) List files

same as unix wildcard notation the equivalent is *.

file_list = glob.glob("*.py") lists all files with python format(extension)

file_list = glob.glob("name*[1-4]*.dat")

-------------------------------------------------------------------------------

5) list all files and folders in folder

file_list = os.listdir("dir name")

file_list = os.listdir(os.curdir) # current folder

file_list.sort() # sort alphabetically

--------------------------------------------------------------------------------------------------------------

6) Check if file or folder exist

if os.path.isfile(filename):

    input_file = open(filename, "r")

and for checking the directory:

if os.path.isdir(directoryname):

    filelist = os.listdir(dirname)

--------------------------------------------------------------------------------------------------------------

7) Remove files

file_list = glob.glob("file_name.pdf")

for file in file_list:

    os.remove(file)

--------------------------------------------------------------------------------------------------------------

8) Remove folder and all its subfolders

shutil.rmtree(foldername) # becareful using this command :)

--------------------------------------------------------------------------------------------------------------

9) Copy file

shutil.copy("fromfile", "tofile")

--------------------------------------------------------------------------------------------------------------

10) Copy folder and all its subfolders

shutil.copytree(sourcefolder, destinationfolder)