Use "os" Module to Manage Files

This section describes methods from the 'os' module for managing files.

"os" module also offers a number of methods to perform read and write operations on files:

Here are examples on how to use above "os" methods.

>>> import os

# Loop through child items of a directory
>>> for child in os.scandir('docs'): child
...
<DirEntry 'conf.py'>
<DirEntry '_templates'>
<DirEntry 'make.bat'>
<DirEntry 'index.rst'>
<DirEntry '_build'>
<DirEntry '_static'>
<DirEntry 'Makefile'>

# write data, rename and read data on a file.
>>> fd = os.open('foo', os.O_CREAT|os.O_WRONLY)
>>> os.write(fd, b'some text')
9
>>> os.close(fd)

>>> os.rename('foo', 'bar')

>>> fd = os.open('bar', os.O_RDONLY)
>>> os.read(fd, 80)
b'some text'
>>> os.close(fd)

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

"sys" and "os" Modules

 What Is "sys" Module

 What Is "os" Module

 Use "os" Module to Create Child Processes

Use "os" Module to Manage Files

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python

 SciPy.org - Python Libraries for Science

 pandas - Data Analysis and Manipulation

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab

 References

 Full Version in PDF/EPUB