GithubHelp home page GithubHelp logo

volkansah / python-command-overview-for-handling-files Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 496 KB

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

License: MIT License

commands commands-cheat-sheet commands-git csv exel files html learning-python pdf python

python-command-overview-for-handling-files's Introduction

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.

python-command-overview-for-handling-files's People

Contributors

volkansah avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.