Python OS
Python Tutorial
This module provides a portable way of using operating system dependent functionality.
- If you just want to read or write a file see open(),
- if you want to manipulate paths, see the os.path module,
- and if you want to read all the lines in all the files on the command line see the fileinput module. For creating temporary files and directories see the tempfile module,
- and for high-level file and directory handling see the shutil module.
Notes on the availability of these functions:
- The design of all built-in operating system dependent modules of Python is such that as long as the same functionality is available, it uses the same interface; for example, the function
os.stat(path)
returns stat information about path in the same format (which happens to have originated with the POSIX interface). - Extensions peculiar to a particular operating system are also available through the os module, but using them is of course a threat to portability.
- All functions accepting path or file names accept both bytes and string objects, and result in an object of the same type, if a path or file name is returned.
- On VxWorks, os.fork, os.execv and os.spawn*p* are not supported.
os.getcwd()
Return a string representing the current working directory.
os.chdir(path)
Change the current working directory to path.
os.listdir(path = ‘.’)
Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries '.'
and '..'
even if they are present in the directory.
os.mkdir(*path, mode=0o777, , dir_fd=None)
Create a directory named path with numeric mode mode.
If the directory already exists, FileExistsError is raised.
os.makedirs(name, mode=0o777, exist_ok=False)
Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory.
os.remove(*path, , dir_fd=None)
Remove (delete) the file path. If path is a directory, an IsADirectoryError is raised. Use rmdir() to remove directories.
This function can support paths relative to directory descriptors.
os.rmdir(*path, , dir_fd=None)
Remove (delete) the directory path. If the directory does not exist or is not empty, an FileNotFoundError or an OSError is raised respectively. In order to remove whole directory trees, shutil.rmtree() can be used.
This function can support paths relative to directory descriptors.
os.removedirs(name)
Remove directories recursively. Works like rmdir() except that, if the leaf directory is successfully removed, removedirs() tries to successively remove every parent directory mentioned in path until an error is raised (which is ignored, because it generally means that a parent directory is not empty). For example, os.removedirs(‘foo/bar/baz’) will first remove the directory ‘foo/bar/baz’, and then remove ‘foo/bar’ and ‘foo’ if they are empty. Raises OSError if the leaf directory could not be successfully removed.
os.rename(*src, dst, , src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst exists, the operation will fail with an OSError subclass in a number of cases:
os.renames(old, new)
Recursive directory or file renaming function. Works like rename(), except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned away using removedirs().
Note: Note This function can fail with the new directory structure made if you lack permissions needed to remove the leaf directory or file.
os.replace(*src, dst, , src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. If dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement).
os.path — Common pathname manipulations
os.path.exists(path)
Return True
if path refers to an existing path or an open file descriptor. Returns False
for broken symbolic links. On some platforms, this function may return False
if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
One More Thing
Python Algorithms - Words: 2,640
Python Crawler - Words: 1,663
Python Data Science - Words: 4,551
Python Django - Words: 2,409
Python File Handling - Words: 1,533
Python Flask - Words: 874
Python LeetCode - Words: 9
Python Machine Learning - Words: 5,532
Python MongoDB - Words: 32
Python MySQL - Words: 1,655
Python OS - Words: 707
Python plotly - Words: 6,649
Python Quantitative Trading - Words: 353
Python Tutorial - Words: 25,451
Python Unit Testing - Words: 68