- Python Basics
- Python - Home
- Python - Overview
- Python - History
- Python - Features
- Python vs C++
- Python - Hello World Program
- Python - Application Areas
- Python - Interpreter
- Python - Environment Setup
- Python - Virtual Environment
- Python - Basic Syntax
- Python - Variables
- Python - Data Types
- Python - Type Casting
- Python - Unicode System
- Python - Literals
- Python - Operators
- Python - Arithmetic Operators
- Python - Comparison Operators
- Python - Assignment Operators
- Python - Logical Operators
- Python - Bitwise Operators
- Python - Membership Operators
- Python - Identity Operators
- Python - Operator Precedence
- Python - Comments
- Python - User Input
- Python - Numbers
- Python - Booleans
- Python Control Statements
- Python - Control Flow
- Python - Decision Making
- Python - If Statement
- Python - If else
- Python - Nested If
- Python - Match-Case Statement
- Python - Loops
- Python - for Loops
- Python - for-else Loops
- Python - While Loops
- Python - break Statement
- Python - continue Statement
- Python - pass Statement
- Python - Nested Loops
- Python Functions & Modules
- Python - Functions
- Python - Default Arguments
- Python - Keyword Arguments
- Python - Keyword-Only Arguments
- Python - Positional Arguments
- Python - Positional-Only Arguments
- Python - Arbitrary Arguments
- Python - Variables Scope
- Python - Function Annotations
- Python - Modules
- Python - Built in Functions
- Python Strings
- Python - Strings
- Python - Slicing Strings
- Python - Modify Strings
- Python - String Concatenation
- Python - String Formatting
- Python - Escape Characters
- Python - String Methods
- Python - String Exercises
- Python Lists
- Python - Lists
- Python - Access List Items
- Python - Change List Items
- Python - Add List Items
- Python - Remove List Items
- Python - Loop Lists
- Python - List Comprehension
- Python - Sort Lists
- Python - Copy Lists
- Python - Join Lists
- Python - List Methods
- Python - List Exercises
- Python Tuples
- Python - Tuples
- Python - Access Tuple Items
- Python - Update Tuples
- Python - Unpack Tuples
- Python - Loop Tuples
- Python - Join Tuples
- Python - Tuple Methods
- Python - Tuple Exercises
- Python Sets
- Python - Sets
- Python - Access Set Items
- Python - Add Set Items
- Python - Remove Set Items
- Python - Loop Sets
- Python - Join Sets
- Python - Copy Sets
- Python - Set Operators
- Python - Set Methods
- Python - Set Exercises
- Python Dictionaries
- Python - Dictionaries
- Python - Access Dictionary Items
- Python - Change Dictionary Items
- Python - Add Dictionary Items
- Python - Remove Dictionary Items
- Python - Dictionary View Objects
- Python - Loop Dictionaries
- Python - Copy Dictionaries
- Python - Nested Dictionaries
- Python - Dictionary Methods
- Python - Dictionary Exercises
- Python Arrays
- Python - Arrays
- Python - Access Array Items
- Python - Add Array Items
- Python - Remove Array Items
- Python - Loop Arrays
- Python - Copy Arrays
- Python - Reverse Arrays
- Python - Sort Arrays
- Python - Join Arrays
- Python - Array Methods
- Python - Array Exercises
- Python File Handling
- Python - File Handling
- Python - Write to File
- Python - Read Files
- Python - Renaming and Deleting Files
- Python - Directories
- Python - File Methods
- Python - OS File/Directory Methods
- Python - OS Path Methods
- Object Oriented Programming
- Python - OOPs Concepts
- Python - Classes & Objects
- Python - Class Attributes
- Python - Class Methods
- Python - Static Methods
- Python - Constructors
- Python - Access Modifiers
- Python - Inheritance
- Python - Polymorphism
- Python - Method Overriding
- Python - Method Overloading
- Python - Dynamic Binding
- Python - Dynamic Typing
- Python - Abstraction
- Python - Encapsulation
- Python - Interfaces
- Python - Packages
- Python - Inner Classes
- Python - Anonymous Class and Objects
- Python - Singleton Class
- Python - Wrapper Classes
- Python - Enums
- Python - Reflection
- Python Errors & Exceptions
- Python - Syntax Errors
- Python - Exceptions
- Python - try-except Block
- Python - try-finally Block
- Python - Raising Exceptions
- Python - Exception Chaining
- Python - Nested try Block
- Python - User-defined Exception
- Python - Logging
- Python - Assertions
- Python - Built-in Exceptions
- Python Multithreading
- Python - Multithreading
- Python - Thread Life Cycle
- Python - Creating a Thread
- Python - Starting a Thread
- Python - Joining Threads
- Python - Naming Thread
- Python - Thread Scheduling
- Python - Thread Pools
- Python - Main Thread
- Python - Thread Priority
- Python - Daemon Threads
- Python - Synchronizing Threads
- Python Synchronization
- Python - Inter-thread Communication
- Python - Thread Deadlock
- Python - Interrupting a Thread
- Python Networking
- Python - Networking
- Python - Socket Programming
- Python - URL Processing
- Python - Generics
- Python Libraries
- NumPy Tutorial
- Pandas Tutorial
- SciPy Tutorial
- Matplotlib Tutorial
- Django Tutorial
- OpenCV Tutorial
- Python Miscellenous
- Python - Date & Time
- Python - Maths
- Python - Iterators
- Python - Generators
- Python - Closures
- Python - Decorators
- Python - Recursion
- Python - Reg Expressions
- Python - PIP
- Python - Database Access
- Python - Weak References
- Python - Serialization
- Python - Templating
- Python - Output Formatting
- Python - Performance Measurement
- Python - Data Compression
- Python - CGI Programming
- Python - XML Processing
- Python - GUI Programming
- Python - Command-Line Arguments
- Python - Docstrings
- Python - JSON
- Python - Sending Email
- Python - Further Extensions
- Python - Tools/Utilities
- Python - GUIs
- Python Useful Resources
- Python Compiler
- NumPy Compiler
- Matplotlib Compiler
- SciPy Compiler
- Python - Questions & Answers
- Python - Online Quiz
- Python - Programming Examples
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python OS File/Directory Methods
The OS module of Python provides a wide range of useful methods to manage files and directories. These are the built-in methods that help in interacting with operating systems. Most of the useful methods are listed here −
Sr.No. | Methods & Description |
---|---|
1 | os.access(path, mode)
Use the real uid/gid to test for access to path. |
2 | os.chdir(path)
Change the current working directory to path |
3 | os.chflags(path, flags)
Set the flags of path to the numeric flags. |
4 | os.chmod(path, mode)
Change the mode of path to the numeric mode. |
5 | os.chown(path, uid, gid)
Change the owner and group id of path to the numeric uid and gid. |
6 | os.chroot(path)
Change the root directory of the current process to path. |
7 | os.close(fd)
Close file descriptor fd. |
8 | os.closerange(fd_low, fd_high)
Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors. |
9 | os.dup(fd)
Return a duplicate of file descriptor fd. |
10 | os.dup2(fd, fd2)
Duplicate file descriptor fd to fd2, closing the latter first if necessary. |
11 | os.fchdir(fd)
Change the current working directory to the directory represented by the file descriptor fd. |
12 | os.fchmod(fd, mode)
Change the mode of the file given by fd to the numeric mode. |
13 | os.fchown(fd, uid, gid)
Change the owner and group id of the file given by fd to the numeric uid and gid. |
14 | os.fdatasync(fd)
Force write of file with filedescriptor fd to disk. |
15 | os.fdopen(fd[, mode[, bufsize]])
Return an open file object connected to the file descriptor fd. |
16 | os.fpathconf(fd, name)
Return system configuration information relevant to an open file. name specifies the configuration value to retrieve. |
17 | os.fstat(fd)
Return status for file descriptor fd, like stat(). |
18 | os.fstatvfs(fd)
Return information about the filesystem containing the file associated with file descriptor fd, like statvfs(). |
19 | os.fsync(fd)
Force write of file with filedescriptor fd to disk. |
20 | os.ftruncate(fd, length)
Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size. |
21 | os.getcwd()
Return a string representing the current working directory. |
22 | os.getcwdu()
Return a Unicode object representing the current working directory. |
23 | os.isatty(fd)
Return True if the file descriptor fd is open and connected to a tty(-like) device, else False. |
24 | os.lchflags(path, flags)
Set the flags of path to the numeric flags, like chflags(), but do not follow symbolic links. |
25 | os.lchmod(path, mode)
Change the mode of path to the numeric mode. |
26 | os.lchown(path, uid, gid)
Change the owner and group id of path to the numeric uid and gid. This function will not follow symbolic links. |
27 | os.link(src, dst)
Create a hard link pointing to src named dst. |
28 | os.listdir(path)
Return a list containing the names of the entries in the directory given by path. |
29 | os.lseek(fd, pos, how)
Set the current position of file descriptor fd to position pos, modified by how. |
30 | os.lstat(path)
Like stat(), but do not follow symbolic links. |
31 | os.major(device)
Extract the device major number from a raw device number. |
32 | os.makedev(major, minor)
Compose a raw device number from the major and minor device numbers. |
33 | os.makedirs(path[, mode])
Recursive directory creation function. |
34 | os.minor(device)
Extract the device minor number from a raw device number. |
35 | os.mkdir(path[, mode])
Create a directory named path with numeric mode mode. |
36 | os.mkfifo(path[, mode])
Create a FIFO (a named pipe) named path with numeric mode mode. The default mode is 0666 (octal). |
37 | os.mknod(filename[, mode=0600, device])
Create a filesystem node (file, device special file or named pipe) named filename. |
38 | os.open(file, flags[, mode])
Open the file file and set various flags according to flags and possibly its mode according to mode. |
39 | os.openpty()
Open a new pseudo-terminal pair. Return a pair of file descriptors (master, slave) for the pty and the tty, respectively. |
40 | os.pathconf(path, name)
Return system configuration information relevant to a named file. |
41 | os.pipe()
Create a pipe. Return a pair of file descriptors (r, w) usable for reading and writing, respectively. |
42 | os.popen(command[, mode[, bufsize]])
Open a pipe to or from command. |
43 | os.read(fd, n)
Read at most n bytes from file descriptor fd. Return a string containing the bytes read. If the end of the file referred to by fd has been reached, an empty string is returned. |
44 | os.readlink(path)
Return a string representing the path to which the symbolic link points. |
45 | os.remove(path)
Remove the file path. |
46 | os.removedirs(path)
Remove directories recursively. |
47 | os.rename(src, dst)
Rename the file or directory src to dst. |
48 | os.renames(old, new)
Recursive directory or file renaming function. |
49 | os.rmdir(path)
Remove the directory path |
50 | os.stat(path)
Perform a stat system call on the given path. |
51 | os.stat_float_times([newvalue])
Determine whether stat_result represents time stamps as float objects. |
52 | os.statvfs(path)
Perform a statvfs system call on the given path. |
53 | os.symlink(src, dst)
Create a symbolic link pointing to src named dst. |
54 | os.tcgetpgrp(fd)
Return the process group associated with the terminal given by fd (an open file descriptor as returned by open()). |
55 | os.tcsetpgrp(fd, pg)
Set the process group associated with the terminal given by fd (an open file descriptor as returned by open()) to pg. |
56 | os.tempnam([dir[, prefix]])
Return a unique path name that is reasonable for creating a temporary file. |
57 | os.tmpfile()
Return a new file object opened in update mode (w+b). |
58 | os.tmpnam()
Return a unique path name that is reasonable for creating a temporary file. |
59 | os.ttyname(fd)
Return a string which specifies the terminal device associated with file descriptor fd. If fd is not associated with a terminal device, an exception is raised. |
60 | os.unlink(path)
Remove the file path. |
61 | os.utime(path, times)
Set the access and modified times of the file specified by path. |
62 | os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
Generate the file names in a directory tree by walking the tree either top-down or bottom-up. |
63 | os.write(fd, str)
Write the string str to file descriptor fd. Return the number of bytes actually written. |