Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Essential File and Directory Operations in Python

Tech 1

Core Path Operations

Path Concatenation

Use the / operator for path joining: path / 'subdirectory'. Initialize paths directly: Path("dir1", "dir2", "filename.txt").

Path Format Conversion

Convert to POSIX-style string: path_obj.as_posix().

Working Directory Management

Current Directory Inspection

import os
print(os.getcwd())

Directory Modification

Change working directory: os.chdir(target_path).

Script Directory Retrieval

Using pathlib:

from pathlib import Path
script_dir = Path(__file__).parent

Using os module:

import os
script_dir = os.path.dirname(__file__)

Set working directory to script's parent:

os.chdir(os.path.dirname(__file__))

String Path Manipulations

Path Joining and Name Extraction

Append components: path_obj.joinpath('new_element'). Extract final component: path_obj.name.

Windows Path Notation

Use raw strings for Windows paths:

win_path = r"C:\Users\Example\Documents\data.txt"

Directory Creation

Recursive Directory Creation

target_path.mkdir(parents=True)

Single-Level Directory Creation

os.mkdir(dir_name)

Path Information Retrieval

Absolute Path Convertion

absolute_path = os.path.abspath(path_string)

Existence Verification

from pathlib import Path
item_path = Path("/target/location")

if item_path.is_file():
    # File exists
if item_path.is_dir():
    # Directory exists
if item_path.exists():
    # Item exists

File Operations

Extension Extraction

filename, extension = os.path.splitext(file_string)

Basename Acquisition

base_name = os.path.basename(full_path)

Filename Without Extension

from pathlib import Path
name_only = Path(__file__).stem

Text File Processing

Reading lines efficiently:

with open(text_file, "r") as file_handle:
    for line_content in file_handle:
        process(line_content)

Writing multiple lines:

with open(output_file, "w") as file_handle:
    file_handle.writelines(line_collection)

JSON File Handling

Writing JSON data:

import json
with open(json_file, "w") as file_handle:
    json.dump(data_structure, file_handle)

Appending JSON data:

with open(json_file, "a") as file_handle:
    json.dump(additional_data, file_handle)

Reading JSON data:

with open(data_file, "r") as file_handle:
    loaded_data = json.load(file_handle)

Directory Management

Directory Removal

import shutil
if os.path.isdir(target_directory):
    shutil.rmtree(target_directory)

Directory Validation

assert os.path.isdir("expected/path")

Immediate Children Iteration

Using pathlib:

parent = Path(directory_path)
for child_item in parent.iterdir():
    print(child_item)

Using os module:

for item_name in os.listdir(root_directory):
    print(item_name)

Recursive Directory Traversal

for current_root, subdirs, file_names in os.walk(start_path):
    for file_name in file_names:
        print(current_root)
        print(os.path.join(current_root, file_name))

File Pattern Matching

Wildcard Patterns

  • *: Matches zero or more characters
  • ?: Matches exactly one character
  • [a-z]: Matches any character in the specified range

Pattern Matching Implementation

import glob
matching_files = glob.glob("directory/*.extension")

Iterative Pattern Matching

glob.iglob() returns an iterator enstead of a complete list.

File Comparison

import filecmp

try:
    identical = filecmp.cmp("first.txt", "second.txt")
    if identical:
        print("Files match")
    else:
        print("Files differ")
except OSError:
    print("File access error occurred")

Temporary File Handling

The tempfile module provides temporary file creation utilities.

Archive Operations

ZIP File Extraction

import zipfile
with zipfile.ZipFile('archive.zip') as zip_archive:
    zip_archive.extractall('destination/')
Tags: Python

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.