Share this page : facebooktwitterlinkedinmailfacebooktwitterlinkedinmail

we can list the contents of a folder or directory, create and remove files by a Python program. Such functions may be really useful for a system or network engineer. Python provides many system functions through a module named os (for “operating system”), which we’ll import for all the programs in this chapter.

Files

Create with open()

Let’s create a text file called oops.txt:

>>> fout = open(‘oops.txt’, ‘wt’)
>>> print(‘Oops, I created a file.’, file=fout)
>>> fout.close()

and close it with close().

Check Existence with exists()

 

>>> import os
>>> os.path.exists(‘oops.txt’)
True

Note that this will always be true:

>>> os.path.exists(‘.’)
True

Check the type

name=’oops.txt’

  • File: os.path.isfile(name)
  • directory: os.path.isdir(name)
  • absolute pathname: os.path.isabs(name)    will be false, os.path.isabs(‘/etc/hosts’) will be true.

 

Copy with copy(), move with move()

 

The copy() function comes from another module, shutil. This example copies the file
oops.txt to the file ohno.txt:

>>> import shutil
>>> shutil.copy(‘oops.txt’, ‘ohno.txt’)

shutil.move() function copies a file and then removes the original

 

remane()

 

>>import os
>>> os.rename(‘ohno.txt’, ‘ohwell.txt’)

hard link or symbolic link

In Unix, a file exists in one place, but it can have multiple names, called links. In lowlevel hard links, it’s not easy to find all the names for a given file.

A symbolic link is an alternative method that stores the new name as its own file, making it possible for you to get both the original and new names at once.

The link() call creates a hard link, and symlink() makes a symbolic link.

The islink() function checks whether the file is a symbolic link.

Here’s how to make a hard link to the existing file oops.txt from the new file yikes.txt:

>> os.link(‘oops.txt’, ‘yikes.txt’)
>>> os.path.isfile(‘yikes.txt’)
True

To create a symbolic link to the existing file oops.txt from the new file jeepers.txt, use the following:

>>>os.path.islink(‘yikes.txt’)
False
>>> os.symlink(‘oops.txt’, ‘jeepers.txt’)
>>> os.path.islink(‘jeepers.txt’)
True

chmod()

 

The command takes an intensely compressed octal (base 8) value that combines user, group, and other permissions.

Binary: 000 000 000 first three bits represent the owner’s permission, then the group’s and others’. For more check out here:http://frankfu.click/linux/linux-filesystem/2/

For instance, to make oops.txt only readable by its owner, first we work out the binary value: 100 000 000, convert to octal: 400.  type the following:

>>> os.chmod(‘oops.txt’, 0o400)

If you don’t want to deal with cryptic octal values and would rather deal with (slightly) obscure cryptic symbols, you can import some constants from the stat module and use a statement such as the following:

>>>import stat
>>> os.chmod(‘oops.txt’, stat.S_IRUSR)

Change Ownership with chown()

This function is also Unix/Linux/Mac–specific. You can change the owner and/or group ownership of a file by specifying the numeric user ID (uid) and group ID (gid):

>>> uid = 5
>>> gid = 22
>>> os.chown(‘oops’, uid, gid)

Get a Pathname with abspath()

This function expands a relative name to an absolute one. If your current directory is / usr/gaberlunzie and the file oops.txt is there, also, you can type the following:

>> os.path.abspath(‘oops.txt’)
‘/usr/gaberlunzie/oops.txt’

Get a symlink Pathname with realpath()

In one of the earlier sections, we made a symbolic link to oops.txt from the new file jeepers.txt. In circumstances such as this, you can get the name of oops.txt from jeepers.txt by using the realpath() function, as shown here:

>> os.path.realpath(‘jeepers.txt’)
‘/usr/gaberlunzie/oops.txt’

Delete a File with remove()

In this snippet, we use the remove() function and say farewell to oops.txt:

> os.remove(‘oops.txt’)
>>> os.path.exists(‘oops.txt’)
False

 

Directory

 

The container of all of these files and directories is a file system (sometimes called a volume).

Create with mkdir()

This example shows how to create a directory called poems to store that precious verse:

>> os.mkdir(‘poems’)
>>> os.path.exists(‘poems’)
True

Delete with rmdir()

Upon second thought, you decide you don’t need that directory after all. Here’s how to delete it:

>> os.rmdir(‘poems’)
>>> os.path.exists(‘poems’)
False

List Contents with listdir()

Let’s make a file, with some contents:

>> os.mkdir(‘poems’)
Now, get a list of its contents (none so far):
>>> os.listdir(‘poems’)
[]

Next, make a subdirectory:

>> os.mkdir(‘poems/mcintyre’)
>>> os.listdir(‘poems’)
[‘mcintyre’]

 

Change Current Directory with chdir()

With this function, you can go from one directory to another. Let’s leave the current directory and spend a little time in poems:

>> import os
>>> os.chdir(‘poems’)

List Matching Files with glob()

The glob() function matches file or directory names by using Unix shell rules rather than the more complete regular expression syntax. Here are those rules:
• * matches everything (re would expect .*)
• ? matches a single character
• [abc] matches character a, b, or c
• [!abc] matches any character except a, b, or c

Try getting all files or directories that begin with m:

>> import glob
>>> glob.glob(‘m*’)
[‘mcintyre’]

How about any two-letter files or directories?

>> glob.glob(‘??’)
[]

An eight-letter word that begins with m and ends with e:

>> glob.glob(‘m??????e’)
[‘mcintyre’]

Anything that begins with a k, l, or m, and ends with e?

>> glob.glob(‘[klm]*e’)
[‘mcintyre’]

Programs and Processes

 

To get the process ID (os.getpid) and the current working directory of the running Python interpreter:

>> import os
>>> os.getpid()
76051
>>> os.getcwd()
‘/Users/williamlubanovic’

Get user ID and group ID

  • os.getuid()
  • os.getgid()
Create a process

To run another program in a shell and grab whatever output it created (both standard output and standard error output), use the getoutput() function.

In a unix/Linux shell, we can ask the shell to return current date by date command:

$ date -u

Mon 28 Nov 2016 11:47:44 UTC

we can also pip the result to a word count command (wc):

# date -u |wc

1       6      29

Then we can get the same result in python program:

>>>import subprocess
>>> ret = subprocess.getoutput(‘date’)
>>> ret
‘Sun Mar 30 22:54:37 CDT 2014’

You won’t get anything back until the process ends. If you need to call something that might take a lot of time, see the discussion on concurrency in “Concurrency”.

Because the argument to getoutput() is a string representing a complete shell command, you can include arguments, pipes, < and > I/O redirection, and so on.

Piping that output string to the wc(word count) command,  which tell the result “one line, six ‘words’ , and 29 characters:”

>> ret = subprocess.getoutput(‘date -u | wc’)
>>> ret
‘ 1 6 29’

A variant method called check_output() takes a list of the command and arguments.
By default it only returns standard output as type bytes rather than a string and does not use the shell:

>>> ret = subprocess.check_output([‘date’, ‘-u’])
>>> ret
b’Mon Mar 31 04:01:50 UTC 2014\n’

In Unix/Linux, every command returns an exit status (sometimes referred to as a return status or exit code). A successful command returns a 0, while an unsuccessful one returns a non-zero value(in the range 1 – 255) that usually can be interpreted as an error code.  For more, check out here http://www.tldp.org/LDP/abs/html/exit-status.html.

To show the exit status of the other program, getstatusoutput() returns a tuple with the status code and output:

>> ret = subprocess.getstatusoutput(‘date’)
>>> ret
(0, ‘Sat Jan 18 21:36:23 CST 2014’)

If you don’t want to capture the output but might want to know its exit status, use call():

> ret = subprocess.call(‘date’)
Sat Jan 18 21:33:11 CST 2014
>>> ret

0

You can run programs with arguments in two ways. The first is to specify them in a single string. Our sample command is date -u, which prints the current date and time in UTC (you’ll read more about UTC in a few pages):

>> ret = subprocess.call(‘date -u’, shell=True)
Tue Jan 21 04:40:04 UTC 2014

You need that shell=True to recognize the command line date -u, splitting it into separate strings and possibly expanding any wildcard characters such as * (we didn’t use any in this example).
The second method makes a list of the arguments, so it doesn’t need to call the shell:

>> ret = subprocess.call([‘date’, ‘-u’])
Tue Jan 21 04:41:59 UTC 2014

 

 

Process terminate

Use method terminate().

 

Create a process with multiprocessing

 

what does ' If __name__ == "__main__"  'means?

It is used to judge if you run a program directly, or importing it in another program.

When you import a module, say the math module, e.g. if you do (at a Python prompt)

>>> import math
>>> print(math.__name__)
math

The __name__ attribute is the name of the module. However, when you run a program directly, instead of importing it, it gets a special __name__ attribute called "__main__". Without a module, you can simply query the __name__ attribute without prefixing it by the module name itself. So, by checking to see if the __name__attribute has been set to "__main__" or not, you can determine if the module has been imported by another module/program, or if it is the script that was first executed.

In this example:

if __name__ == "__main__":
    main()

This translates to: “if this file was run directly (not imported), then run the ‘main()’ function.”

The simplest way to spawn a second is to instantiate a Process object with a target function and call start() to let it begin working.

Now let’s take a look at a more complex example:

import multiprocessing
import os

def do_this(what):
    whoami(what)

def whoami(what):
    print("Process %s says: %s" % (os.getpid(), what))

if __name__ == "__main__":
    whoami("I'm the main program")
    for n in range(4):
        p = multiprocessing.Process(target=do_this,
            args=("I'm function %s" % n,))
        p.start()

Then the result will be something similar to ( the process ID may be different):

process 827 syas: I’m the main program

process 828 syas: I’m function 0

process 829 syas: I’m function 1

process 830 syas: I’m function 2

process 831 syas: I’m function 3

And another example that follows, our process would count to a million, sleeping at each step for a second, and printing an irritating message. However, our main program runs out of patience in 6 seconds and nukes it from orbit:

import multiprocessing

import time

import os

def whoami(name):

print(“I’m %s, in process %s” %(name, os.getpid()))

 

def loopy(name):

whoami(name)

start=1

stop=1000000

 

# This loop will execute every 1 second.

for num in range(start,stop):

print(“\tNumber %s of %s, Honk!”% (num,stop))

time.sleep(1)

 

if __name__==”__main__”:

whoami(“main”)

p=multiprocessing.Process(target=loopy, args=(“loopy”,))

p.start()

# the main process sleep for 6 seconds,so the other process can execute for 6 second.

#Because the For loop will execute every 1 second, so the for loop will execute 6 times (6/1=6)

#after 6 seconds, the main process will terminate. 

time.sleep(6)

p.terminate()

 

 

Calendars and clocks

 

You can convert dates and times to strings by using strftime().

strftime() has two arguments, first argument is a customized string with format strings, second argument is a time object.

strftime() uses format strings to specify the output, which you can see in table bellow.

Format string Date/time unit  Range
%Y year 1900-
%m month (number) 01-12
%B month name January…
%b month abbrev Jan,Feb…
%d day of month 01-31
%A weekday name Sunday,…
%a weekday abbrev Sun,Mon…
%H Hour(24 hr) 0-23
%I hour (12hr) 0-12
%p AM/PM AM,PM
%M minute 00-59
%S Second 00-59

 

E.g.

>>> import time
>>> fmt = “It’s %A, %B %d, %Y, local time %I:%M:%S%p”
>>> t = time.localtime()
>>> t
time.struct_time(tm_year=2014, tm_mon=2, tm_mday=4, tm_hour=19,
tm_min=28, tm_sec=38, tm_wday=1, tm_yday=35, tm_isdst=0)
>>> time.strftime(fmt, t)
“It’s Tuesday, February 04, 2014, local time 07:28:38PM”

If we try this with a date object, only the date parts will work, and the time defaults to midnight:

>>>from datetime import date
>>> some_day = date(2014, 7, 4)
>>> fmt = “It’s %B %d, %Y, local time %I:%M:%S%p”
>>> some_day.strftime(fmt)
“It’s Friday, July 04, 2014, local time 12:00:00AM”

For a time object, only the time parts are converted:

>>>from datetime import time
>>> some_time = time(10, 35)
>>> some_time.strftime(fmt)
“It’s Monday, January 01, 1900, local time 10:35:00AM”

To print different month and day names in different languages, change your locale by using setlocale(); its first argument is locale.LC_TIME for dates and times, and the second is a string combining the language and country abbreviation.

>>> import locale
>>> from datetime import date
>>> halloween = date(2014, 10, 31)
>>> for lang_country in [‘en_us’, ‘fr_fr’, ‘de_de’, ‘es_es’, ‘is_is’,]:
… locale.setlocale(locale.LC_TIME, lang_country)
… halloween.strftime(‘%A, %B %d’)

‘en_us’
‘Friday, October 31’
‘fr_fr’
‘Vendredi, octobre 31’
‘de_de’
‘Freitag, Oktober 31’
‘es_es’
‘viernes, octubre 31’
‘is_is’
‘föstudagur, október 31’
>>>

Where do you find these magic values for lang_country? This is a bit wonky, but you
can try this to get all of them (there are a few hundred):

>>>import locale
>>> names = locale.locale_alias.keys()

 

a two-character language code followed by an underscore and a two-character country code:

>>> good_names = [name for name in names if \

len(name) == 5 and name[2] == ‘_’]

What do the first five look like?

>> good_names[:5] [‘sr_cs’, ‘de_at’, ‘nl_nl’, ‘es_ni’, ‘sp_yu’]

So, if you wanted all the German language locales, try this:

>> de = [namefor name in good_names if name.startswith(‘de’)] >>> de
[‘de_at’, ‘de_de’, ‘de_ch’, ‘de_lu’, ‘de_be’]