Example HTML page

Python OS module

OS Module

The Python OS module works with files and directories especially.

To import os module

>>> import os
>>> dir(os)
['F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fsdecode', 'fsencode', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

There are having many methods of os module. Following are some of the methods of OS module.

1.os.access(path,mode)

This method uses the real uid/gid to test for access to a path. If access is allowed, it returns True. Else, it returns False. The first argument is the path; the second is the mode. The mode can take one of four values:

  1. os.F_OK  — Found
  2. os.R_OK  — Readable
  3. os.W_OK  — Writable
  4. os.X_OK  — Executable

Ex

>>> os.chdir(“C:\python”)

>>> os.getcwd()

‘C:\\python’

>>> os.access(‘thefile.txt’,os.F_OK)

True

>>> os.access(‘thefile.txt’,os.R_OK)

True

>>> os.access(‘thefile.txt’,os.W_OK)

True

>>> os.access(‘thefile.txt’,os.X_OK)

True

Note: For working with OS module I have chosen a file which was already created (‘thefile.txt’).

2. os.chdir(path)

This will change the current working directory to the path we specified.

Ex 

>>> os.chdir(‘C:\python’)

‘C:\python’ is not the default path we can set as per our need. It prints nothing. If you want to check if the directory is changed or not, then

>>> os.getcwd()

‘C:\\python’

3.os.close(fd)

It closes the associated file with descriptor fd.

Ex

>>> fd=os.open(‘thefile.txt’,os.O_RDWR)

>>> os.close(fd)

Here it returns no value.

4.os.listdir(path)

This will return a list holding the names of the entries in the directory at the path.

Ex

>>> path='C:\Softwares'
>>> dir=os.listdir(path)
>>> for file in dir:
	print(file)

	
32bit
64bit
apache-tomcat-8.0.18.exe
jdk-8u11-nb-8-windows-x64.exe
jdk-8u60-windows-x64.exe
myeclipse-8.6.1-win32.exe
mysql-gui-tools-5.0-r4-win32.msi
mysql-installer-community-5.6.20.0.msi
netbeans-8.1-windows.exe

5.  os.pipe( )

It creates a pipe. Then, it returns a pair of descriptors- r & w- for reading and writing.

Ex

>>> os.pipe()

(12, 13)

6. os.stat(path)

This Python os Module performs a stat system call on the specified path.

These are the members of the stat structure:

  • st_mode − protection bits
  • st_ino − inode number
  • st_dev − device
  • st_nlink − number of hard links
  • st_uid − user id of owner
  • st_gid − group id of owner
  • st_size − size of file, in bytes
  • st_atime − time of most recent access
  • st_mtime − time of most recent content modification
  • st_ctime − time of most recent metadata change.

Ex

>>> stat=os.stat(‘purchase1.py’)
>>> stat
os.stat_result(st_mode=33206, st_ino=562949953580744, st_dev=3457331182, st_nlink=1, st_uid=0, st_gid=0, st_size=334, st_atime=1518192184, st_mtime=1533064593, st_ctime=1518192184)

 

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest