Skip to content

Commit 008be06

Browse files
authored
Update README.md
1 parent fe4394f commit 008be06

File tree

1 file changed

+156
-1
lines changed

1 file changed

+156
-1
lines changed

README.md

+156-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,157 @@
1-
# usefull-Python-Command-Overview
1+
# Usefull Python Command Overview
2+
23
This project serves as a comprehensive guide to executing shell commands and handling files using Python.
4+
Installation. You can install Python through the official website: https://www.python.org/downloads/
5+
## Usage
6+
This project can be used as a reference guide for executing shell commands and file handling in Python.
7+
Shell Commands with Python
8+
9+
## Install requiments
10+
```python
11+
pip install PyPDF2 python-docx pandas beautifulsoup4
12+
```
13+
14+
This project makes use of Python to execute shell commands. Here are some of the Python commands used in this project:
15+
16+
Creating and writing to a file:
17+
18+
```python
19+
20+
with open("filename.txt", "w") as f:
21+
f.write("Hello, World!")
22+
```
23+
Reading from a file:
24+
25+
```python
26+
27+
with open("filename.txt", "r") as f:
28+
print(f.read())
29+
```
30+
Appending to an existing file:
31+
32+
```python
33+
34+
with open("filename.txt", "a") as f:
35+
f.write("More text.")
36+
```
37+
Deleting a file:
38+
39+
```python
40+
41+
import os
42+
os.remove("filename.txt")
43+
```
44+
Checking if a file exists:
45+
46+
```python
47+
48+
import os
49+
os.path.exists("filename.txt")
50+
```
51+
Creating a directory:
52+
53+
```python
54+
import os
55+
os.mkdir("directory_name")
56+
```
57+
Large File Handling
58+
59+
Working with large files requires a different set of Python commands. Here are some examples:
60+
61+
Opening a file:
62+
63+
```python
64+
65+
file = open('large_file.txt', 'r')
66+
```
67+
Reading a file line by line:
68+
69+
```python
70+
71+
with open('large_file.txt', 'r') as file:
72+
for line in file:
73+
print(line)
74+
```
75+
Reading a specific number of lines:
76+
77+
```python
78+
79+
from itertools import islice
80+
with open('large_file.txt', 'r') as file:
81+
head = list(islice(file, 5))
82+
```
83+
Searching within a large file:
84+
85+
```python
86+
87+
with open('large_file.txt', 'r') as file:
88+
for line in file:
89+
if 'some_text' in line:
90+
print(line)
91+
```
92+
Writing to a large file:
93+
94+
```python
95+
96+
with open('large_file.txt', 'w') as file:
97+
file.write('some_text')
98+
```
99+
Splitting files:
100+
101+
```python
102+
103+
chunk_size = 1000000 # 1 MB
104+
with open('large_file.txt', 'r') as file:
105+
chunk = file.read(chunk_size)
106+
while chunk:
107+
with open('chunk.txt', 'w') as chunk_file:
108+
chunk_file.write(chunk)
109+
chunk = file.read(chunk_size)
110+
```
111+
Handling Specific File Formats
112+
113+
Python can read various file formats using specific libraries. Here are some examples:
114+
115+
Reading PDF files:
116+
117+
```python
118+
119+
import PyPDF2
120+
121+
with open('example.pdf', 'rb') as file:
122+
reader = PyPDF2.PdfFileReader(file)
123+
page = reader.getPage(0)
124+
print(page.extract_text())
125+
```
126+
Reading Word documents:
127+
128+
```python
129+
130+
from docx import Document
131+
132+
doc = Document('example.docx')
133+
for para in doc.paragraphs:
134+
print(para.text)
135+
```
136+
Reading Excel files:
137+
138+
```python
139+
140+
import pandas as pd
141+
142+
data = pd.read_excel('example.xlsx')
143+
print(data)
144+
```
145+
Reading HTML files:
146+
147+
```python
148+
149+
from bs4 import BeautifulSoup
150+
151+
with open("example.html") as fp:
152+
soup = BeautifulSoup(fp, 'html.parser')
153+
print(soup.prettify())
154+
```
155+
Credits
156+
157+
This README was generated with the help of ChatGPT, an AI developed by OpenAI.

0 commit comments

Comments
 (0)