Skip to content

This project serves as a comprehensive guide to executing shell commands and handling files using Python.

License

Notifications You must be signed in to change notification settings

VolkanSah/Python-Command-Overview-for-handling-files

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Python Command Overview for handling files

Webview Python comman overview for big files

This project serves as a comprehensive guide to executing shell commands and handling files using Python. Installation.

Table of Contents

Installation

You can install Python through the official website: https://www.python.org/downloads/

Requiments

pip install PyPDF2 python-docx pandas beautifulsoup4

Usage

This project can be used as a reference guide for executing shell commands and file handling in Python. Shell Commands with Python

This project makes use of Python to execute shell commands. Here are some of the Python commands used in this project:

Creating and writing to a file

with open("filename.txt", "w") as f:
    f.write("Hello, World!")

Reading from a file

with open("filename.txt", "r") as f:
    print(f.read())

Appending to an existing file

with open("filename.txt", "a") as f:
    f.write("More text.")

Deleting a file

import os
os.remove("filename.txt")

Checking if a file exists

import os
os.path.exists("filename.txt")

Creating a directory

import os
os.mkdir("directory_name")

Large File Handling

Working with large files requires a different set of Python commands. Here are some examples:

Opening a file

file = open('large_file.txt', 'r')

Reading a file line by line

with open('large_file.txt', 'r') as file:
    for line in file:
        print(line)

Reading a specific number of lines

from itertools import islice
with open('large_file.txt', 'r') as file:
    head = list(islice(file, 5))

Searching within a large file

with open('large_file.txt', 'r') as file:
    for line in file:
        if 'some_text' in line:
            print(line)

Writing to a large file

with open('large_file.txt', 'w') as file:
    file.write('some_text')

Splitting files

chunk_size = 1000000  # 1 MB
with open('large_file.txt', 'r') as file:
    chunk = file.read(chunk_size)
    while chunk:
        with open('chunk.txt', 'w') as chunk_file:
            chunk_file.write(chunk)
        chunk = file.read(chunk_size)

Handling Specific File Formats

Python can read various file formats using specific libraries. Here are some examples:

Reading PDF files

import PyPDF2

with open('example.pdf', 'rb') as file:
    reader = PyPDF2.PdfFileReader(file)
    page = reader.getPage(0)
    print(page.extract_text())

Reading Word documents

from docx import Document

doc = Document('example.docx')
for para in doc.paragraphs:
    print(para.text)

Reading Excel files

import pandas as pd

data = pd.read_excel('example.xlsx')
print(data)

Reading HTML files

from bs4 import BeautifulSoup

with open("example.html") as fp:
    soup = BeautifulSoup(fp, 'html.parser')
print(soup.prettify())

Credits

This README was generated with the help of ChatGPT4, an AI developed by OpenAI & Volkan Sah.

Usefull

❤️ Thank you for your support!

If you appreciate my work, please consider supporting me:

  • Become a Sponsor: Link to my sponsorship page
  • ⭐ my projects: Starring projects on GitHub helps increase their visibility and can help others find my work.
  • Follow me: Stay updated with my latest projects and releases.