The OS Module

Download Report

Transcript The OS Module

The OS Module
(windows and unix)
Dick Steflik
CS360
Python OS Module
The OS module provides a portable way of using operating system dependent
functionality. For instance to get a directory list on a windows machine you can
issue :
os.system(‘ls –l’)
will return a unix style list:
drwxr-xr-x
-rw-r--r--
2 Dick
1 Dick
Administ
Administ
0 Feb 27 22:54 aaaa
645 Feb 28 22:41 ftp.py
os.system(‘dir *.*’)
will return windows style list
Volume in drive C has no label.
Volume Serial Number is F017-710B
Directory of C:\cs260\ftp
02/27/2012
02/27/2012
02/27/2012
02/28/2012
10:54 PM
<DIR>
.
10:54 PM
<DIR>
..
10:54 PM
<DIR>
aaaa
10:41 PM
645 ftp.py
1 File(s)
645 bytes
3 Dir(s) 44,729,069,568 bytes free
OS Module – Misc Interfaces
os.name – The name of the operating system module imported (posix,nt, os2, ce , java , riscos)
os.getpid – returns the process id.
os.getenv(varname [, value]) – return the value of the environment variable varname.
os.putenv(varname[,value]) – set the named environment variable to value.
os.unsetenv(varname) – delete the named environment variable.
File Descriptors
An abstract indicator for accessing a file. In POSIX (Portable Operating System Interface) a
descriptor is a C type int. In Microsoft terminology it is known as a file handle.
POSIX has three standard file descriptors that are available to every process:
0
Standard Input (stdin)
1
Standard Output (stdout)
2
Standard Error (stderr)
Generally , a descriptor is an index into a kernel resident data structure that keeps trace of all open
files. In POSIX this is called the file descriptor table and each process has it’s own file descriptor
table. In UNIX and Linux systems a file descriptor can refer to: files, directories, bloc and/or
character devices, sockets, and named or unnamed pipes (FIFOs)
Windows uses the term file handles. To provide POSIX compatibility Windows C libraries provide
wrappers around native APIs to provide POSIX like integer file descriptors.
OS Module Flags
os.O_RDONLY: open for reading only
os.O_WRONLY: open for writing only
os.O_RDWR : open for reading and writing
os.O_NONBLOCK: do not block on open
os.O_APPEND: append on each write
os.O_CREAT: create file if it does not exist
os.O_TRUNC: truncate size to 0
os.O_EXCL: error if create and file exists
os.O_SHLOCK: atomically obtain a shared lock
os.O_EXLOCK: atomically obtain an exclusive lock
os.O_DIRECT: eliminate or reduce cache effects
os.O_FSYNC : synchronous writes
os.O_NOFOLLOW: do not follow symlinks
OS Module File Tools
Additional file processing tools, different than the Python file object.
os.open(path, flags, mode) – opens a file and returns its file descriptor
os,read(file_descriptor, N)
os.write(file_descriptor, string)
os.lseek(file_descriptor, pos) – set the current position of the file_descriptor to position pos
os.close(file_descriptor) – closes the file_descriptor
os.fstat(file_descriptor) – return the status of file_descriptor.
os.fsync(file_descriptor) – Force write of file with file_descriptor to disk.
Example
import os, sys
# Open a file
fd = os.open( "foo.txt", os.O_RDWR|os.O_CREAT )
# Write one string
os.write(fd, "This is test")
# Close opened file
os.close( fd )
print "Closed the file successfully!!"
OS Module Directory Tools
os.chdir(path) – change the current working directory to path
os.getcwd() – get current working directory
os.getcwdu() – get the current working directory as a unicode object
os.listdir(path) – returns a list of the names of the entries in the directory given by path; list
is in arbitrary order and doesn’t contain ‘,’ or ‘..’ .
>>> print os.listdir('.')
['aaaa', 'ftp.py']
os.mkdir(path[,mode]) – create a directory named path with mode mode. default mode is
0777. Mode is ignored on Windows.
os.rename(old , new ) – rename file or directory from old to new.
os.rmdir(path) – remove (delete) the directory path. This only works if the directory is
empty.