diff --git a/Python/ch4.File Handling/Work with Binary File_1.py b/Python/ch4.File Handling/Work with Binary File_1.py new file mode 100644 index 0000000..4b872c6 --- /dev/null +++ b/Python/ch4.File Handling/Work with Binary File_1.py @@ -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 "", line 1, in +IndexError: index out of range +>>> type(a) + +>>> list(a) +[2, 161, 128] \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Binary File_2.py b/Python/ch4.File Handling/Work with Binary File_2.py new file mode 100644 index 0000000..28089c9 --- /dev/null +++ b/Python/ch4.File Handling/Work with Binary File_2.py @@ -0,0 +1,3 @@ +>>> a = b'ab_*' +>>> list(a) +[97, 98, 95, 42] \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Binary File_3.py b/Python/ch4.File Handling/Work with Binary File_3.py new file mode 100644 index 0000000..85abda2 --- /dev/null +++ b/Python/ch4.File Handling/Work with Binary File_3.py @@ -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 "", line 1, in +ValueError: bytes must be in range(0, 256) \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Binary File_4.py b/Python/ch4.File Handling/Work with Binary File_4.py new file mode 100644 index 0000000..f57dd00 --- /dev/null +++ b/Python/ch4.File Handling/Work with Binary File_4.py @@ -0,0 +1,4 @@ +with open('src.png', 'rb') as src: + data = src.read() + with open('dst.png', 'wb') as dst: + dst.write(data) \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Text File_1.py b/Python/ch4.File Handling/Work with Text File_1.py new file mode 100644 index 0000000..c81c78c --- /dev/null +++ b/Python/ch4.File Handling/Work with Text File_1.py @@ -0,0 +1,6 @@ +# Reading Characters of File +>>> f = open("info.txt", "r") +>>> f.read(6) +'Hello;' +>>> f.read(6) +'\nI am ' \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Text File_2.py b/Python/ch4.File Handling/Work with Text File_2.py new file mode 100644 index 0000000..b929668 --- /dev/null +++ b/Python/ch4.File Handling/Work with Text File_2.py @@ -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 = '') \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Text File_3.py b/Python/ch4.File Handling/Work with Text File_3.py new file mode 100644 index 0000000..79f5a5a --- /dev/null +++ b/Python/ch4.File Handling/Work with Text File_3.py @@ -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') \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Text File_4.py b/Python/ch4.File Handling/Work with Text File_4.py new file mode 100644 index 0000000..062ed49 --- /dev/null +++ b/Python/ch4.File Handling/Work with Text File_4.py @@ -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) \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Text File_5.py b/Python/ch4.File Handling/Work with Text File_5.py new file mode 100644 index 0000000..9b5074d --- /dev/null +++ b/Python/ch4.File Handling/Work with Text File_5.py @@ -0,0 +1,4 @@ +# Closing File +with open("file address", "opening mode") as f: + #tasks to do with file + pass \ No newline at end of file diff --git a/Python/ch4.File Handling/Work with Text File_6.py b/Python/ch4.File Handling/Work with Text File_6.py new file mode 100644 index 0000000..f876840 --- /dev/null +++ b/Python/ch4.File Handling/Work with Text File_6.py @@ -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 \ No newline at end of file diff --git a/Python/ch4.File Handling/info.txt b/Python/ch4.File Handling/info.txt new file mode 100644 index 0000000..b3648b3 --- /dev/null +++ b/Python/ch4.File Handling/info.txt @@ -0,0 +1,2 @@ +Hello; +I am Mohamadreza. \ No newline at end of file diff --git a/Python/ch4.File Handling/opening file_1.py b/Python/ch4.File Handling/opening file_1.py new file mode 100644 index 0000000..d688200 --- /dev/null +++ b/Python/ch4.File Handling/opening file_1.py @@ -0,0 +1 @@ +f = open("file address", "opening mode") \ No newline at end of file diff --git a/Python/ch4.File Handling/opening file_2.py b/Python/ch4.File Handling/opening file_2.py new file mode 100644 index 0000000..c030e6e --- /dev/null +++ b/Python/ch4.File Handling/opening file_2.py @@ -0,0 +1,6 @@ +# File Object +>>> f = open('a.txt') +>>> f +<_io.TextIOWrapper name='a.txt' mode='r' encoding='UTF-8'> +>>> type(f) + \ No newline at end of file