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

  1. Python - Create new file
  2. Python - Open a file
  3. Python - Read file
  4. Python - Write to file
  5. Python - Append to file
  6. Python - Delete file
  7. Python - Rename file
  8. Python - Copy file
  9. Python - Print to file
  10. Python - Print from file
  11. Python - Replace file extension

File modes

  1. Python - Open file in read mode
  2. Python - Open file in write mode
  3. Python - Open file in append mode
  4. Python - Open file in text mode
  5. Python - Open file in binary mode

Directory or Folder Operations

  1. Python - Create a Directory
  2. Python - Remove directory
  3. Python - Rename directory
  4. Python - Copy directory
  5. Python - Get list of files in a directory
  6. Python - Get list of all files in a directory and its sub-directories recursively

Checks

  1. Python - Check if file exists
  2. Python - Check if directory exists
  3. Python - Check if file is writable
  4. Python - Check if file is readable
  5. Python - Check if file is empty
  6. Python - Check if directory is empty
  7. Python - Check if specified path is file or directory

File Meta Information

  1. Python - Get file size
  2. Python - Get file extension
  3. Python - Get file created time
  4. Python - Get file last modified time
  5. Python - Get file last access time
  6. Python - Update file last modified time

Text File Operations

  1. Python - Write string to text file
  2. Python - Write list of strings as lines in text file
  3. Python - Find unique words in text tile
  4. Python - Append text to file
  5. Python - Read file line by line
  6. Python - Replace a string in text file
  7. Python - Replace multiple spaces with single space in a text file
  8. Python - Resize or truncate text file to specific size
  9. Python - Count number of words in text tile
  10. Python - Count number of characters in a text file
  11. Python - Count occurrences of a word in text file

CSV File Operations

  1. Python - Read CSV file

Error Handling

  1. Handling Python FileNotFoundError

Summary

In this tutorial of Python Examples, we learned about some of the file operations that we can perform on files and directories.




Python Libraries