Python File Operations - Tutorials and Examples
File Operations
It is very important for any programming language to interact with the File System and be able to do all the read, write, update, append and delete operations on the files and folders/directories.
File Operation Tutorials
The following tutorials cover file operations like read, write, append, update, and delete on files, directories, etc., using Python programming.
CRUD Operations - Files
- Python - Create new file
- Python - Open a file
- Python - Read file
- Python - Write to file
- Python - Append to file
- Python - Delete file
- Python - Rename file
- Python - Copy file
- Python - Print to file
- Python - Print from file
- Python - Replace file extension
File modes
- Python - Open file in read mode
- Python - Open file in write mode
- Python - Open file in append mode
- Python - Open file in text mode
- Python - Open file in binary mode
Directory or Folder Operations
- Python - Create a Directory
- Python - Remove directory
- Python - Rename directory
- Python - Copy directory
- Python - Get list of files in a directory
- Python - Get list of all files in a directory and its sub-directories recursively
Checks
- Python - Check if file exists
- Python - Check if directory exists
- Python - Check if file is writable
- Python - Check if file is readable
- Python - Check if file is empty
- Python - Check if directory is empty
- Python - Check if specified path is file or directory
File Meta Information
- Python - Get file size
- Python - Get file extension
- Python - Get file created time
- Python - Get file last modified time
- Python - Get file last access time
- Python - Update file last modified time
Text File Operations
- Python - Write string to text file
- Python - Write list of strings as lines in text file
- Python - Find unique words in text tile
- Python - Append text to file
- Python - Read file line by line
- Python - Replace a string in text file
- Python - Replace multiple spaces with single space in a text file
- Python - Resize or truncate text file to specific size
- Python - Count number of words in text tile
- Python - Count number of characters in a text file
- Python - Count occurrences of a word in text file
CSV File Operations
Error Handling
Summary
In this tutorial of Python Examples, we learned about some of the file operations that we can perform on files and directories.