Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-robot77 committed Jul 5, 2023
1 parent 4e2d446 commit 9000c17
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Python/ch4.File Handling/Work with Binary File_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
>>> a = b'\x02\xA1\x80'
>>> a
b'\x02\xa1\x80'
>>> a[0]
2
>>> a[1]
161
>>> a[2]
128
>>> a[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index out of range
>>> type(a)
<class 'bytes'>
>>> list(a)
[2, 161, 128]
3 changes: 3 additions & 0 deletions Python/ch4.File Handling/Work with Binary File_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>>> a = b'ab_*'
>>> list(a)
[97, 98, 95, 42]
11 changes: 11 additions & 0 deletions Python/ch4.File Handling/Work with Binary File_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
>>> a = bytes([1, 129, 231])
>>> a
b'\x01\x81\xe7'
>>> list(a)
[1, 129, 231]
>>> a[1:3]
b'\x81\xe7'
>>> a = bytes([1, 260])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: bytes must be in range(0, 256)
4 changes: 4 additions & 0 deletions Python/ch4.File Handling/Work with Binary File_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
with open('src.png', 'rb') as src:
data = src.read()
with open('dst.png', 'wb') as dst:
dst.write(data)
6 changes: 6 additions & 0 deletions Python/ch4.File Handling/Work with Text File_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Reading Characters of File
>>> f = open("info.txt", "r")
>>> f.read(6)
'Hello;'
>>> f.read(6)
'\nI am '
11 changes: 11 additions & 0 deletions Python/ch4.File Handling/Work with Text File_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Reading Lines of File
f = open("Info.txt", "r")
lines = []
while True:
line = f.readline()
if line == '':
break # end of file
lines.append(line)
lines.reverse()
for line in lines:
print(line, end = '')
6 changes: 6 additions & 0 deletions Python/ch4.File Handling/Work with Text File_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Writing in File
n = int(input())
f = open("dst.txt", "w")
for i in range(n):
line = input()
f.write(line + '\n')
18 changes: 18 additions & 0 deletions Python/ch4.File Handling/Work with Text File_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# File Pointer
>>> f = open("test.txt", "w")
>>> f.tell()
0
>>> f.write("salam")
5
>>> f.tell()
5
>>> f.write("ali")
3
>>> f.tell()
8

f = open("a.txt", "r")
l, r = (int(x) for x in input().split())
f.seek(l)
s = f.read(r - l + 1)
print(s)
4 changes: 4 additions & 0 deletions Python/ch4.File Handling/Work with Text File_5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Closing File
with open("file address", "opening mode") as f:
#tasks to do with file
pass
10 changes: 10 additions & 0 deletions Python/ch4.File Handling/Work with Text File_6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Application of Generators in Files
def csv_reader(path):
file = open(path)
result = file.read().split("\n")
return result

def csv_reader(path):
with open(path) as file:
for row in file:
yield row
2 changes: 2 additions & 0 deletions Python/ch4.File Handling/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Hello;
I am Mohamadreza.
1 change: 1 addition & 0 deletions Python/ch4.File Handling/opening file_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f = open("file address", "opening mode")
6 changes: 6 additions & 0 deletions Python/ch4.File Handling/opening file_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# File Object
>>> f = open('a.txt')
>>> f
<_io.TextIOWrapper name='a.txt' mode='r' encoding='UTF-8'>
>>> type(f)
<class '_io.TextIOWrapper'>

0 comments on commit 9000c17

Please sign in to comment.