|
| 1 | +#File Objects |
| 2 | + |
| 3 | +##The Basics: |
| 4 | +#f = open("test.txt", "r") |
| 5 | +#f = open("test.txt", "w") |
| 6 | +#f = open("test.txt", "a") |
| 7 | +#f = open("test.txt", "r+") |
| 8 | +#print(f.name) |
| 9 | +#print(f.mode) |
| 10 | +#f.close() |
| 11 | + |
| 12 | +##Reading Files: |
| 13 | +#with open("test.txt", "r") as f: |
| 14 | + #pass |
| 15 | + |
| 16 | + ##Small Files: |
| 17 | + #f_contents = f.read() |
| 18 | + #print(f_contents) |
| 19 | + |
| 20 | + ##Big Files: |
| 21 | + #f_contents = f.readlines() |
| 22 | + #print(f_contents) |
| 23 | + |
| 24 | + ###With the extra lines: |
| 25 | + #f_contents = f.readline() |
| 26 | + #print(f_contents) |
| 27 | + #f_contents = f.readline() |
| 28 | + #print(f_contents) |
| 29 | + |
| 30 | + ###Without the extra lines: |
| 31 | + #f_contents = f.readline() |
| 32 | + #print(f_contents, end = '') |
| 33 | + #f_contents = f.readline() |
| 34 | + #print(f_contents, end = '') |
| 35 | + |
| 36 | + ###Iterating through the file: |
| 37 | + #for line in f: |
| 38 | + #print(line, end = '') |
| 39 | + |
| 40 | + ###Going Back....: |
| 41 | + #f_contents = f.read() |
| 42 | + #print(f_contents, end = '') |
| 43 | + |
| 44 | + ###Printing by characters: |
| 45 | + #f_contents = f.read(100) |
| 46 | + #print(f_contents, end = '') |
| 47 | + #f_contents = f.read(100) |
| 48 | + #print(f_contents, end = '') |
| 49 | + #f_contents = f.read(100) |
| 50 | + #print(f_contents, end = '') |
| 51 | + |
| 52 | + ###Iterating through small chunks: |
| 53 | + #size_to_read = 100 |
| 54 | + #f_contents = f.read(size_to_read) |
| 55 | + #while len(f_contents) > 0: |
| 56 | + #print(f_contents) |
| 57 | + #f_contents = f.read(size_to_read) |
| 58 | + |
| 59 | + ###Iterating through small chunks, with 10 characters: |
| 60 | + #size_to_read = 10 |
| 61 | + #f_contents = f.read(size_to_read) |
| 62 | + #print(f_contents, end = '') |
| 63 | + #f.seek(0) |
| 64 | + #f_contents = f.read(size_to_read) |
| 65 | + #print(f_contents, end = '') |
| 66 | + #print(f.tell()) |
| 67 | + #while len(f_contents) > 0: |
| 68 | + #print(f_contents, end = '*') |
| 69 | + #f_contents = f.read(size_to_read) |
| 70 | +#print(f.mode) |
| 71 | +#print(f.closed) |
| 72 | +#print(f.read()) |
| 73 | + |
| 74 | + |
| 75 | +##Writing Files: |
| 76 | +###The Error: |
| 77 | +#with open("test.txt", "r") as f: |
| 78 | + #f.write("Test") |
| 79 | + |
| 80 | +###Writing Starts: |
| 81 | +#with open("test2.txt", "w") as f: |
| 82 | + #pass |
| 83 | + #f.write("Test") |
| 84 | + #f.seek(0) |
| 85 | + #f.write("Test") |
| 86 | + #f.seek("R") |
| 87 | + |
| 88 | +##Copying Files: |
| 89 | +#with open("test.txt", "r") as rf: |
| 90 | + #with open("test_copy.txt", "w") as wf: |
| 91 | + #for line in rf: |
| 92 | + #wf.write(line) |
| 93 | + |
| 94 | +#Copying the/your image: |
| 95 | +###The Error |
| 96 | +#with open("bronx.jpg", "r") as rf: |
| 97 | + #with open("bronx_copy.jpg", "w") as wf: |
| 98 | + #for line in rf: |
| 99 | + #wf.write(line) |
| 100 | + |
| 101 | +###Copying the image starts, without chunks: |
| 102 | +#with open("bronx.jpg", "rb") as rf: |
| 103 | + #with open("bronx_copy.jpg", "wb") as wf: |
| 104 | + #for line in rf: |
| 105 | + #wf.write(line) |
| 106 | + |
| 107 | +###Copying the image with chunks: |
| 108 | +#with open("bronx.jpg", "rb") as rf: |
| 109 | + #with open("bronx_copy.jpg", "wb") as wf: |
| 110 | + #chunk_size = 4096 |
| 111 | + #rf_chunk = rf.read(chunk_size) |
| 112 | + #while len(rf_chunk) > 0: |
| 113 | + #wf.write(rf_chunk) |
| 114 | + #rf_chunk = rf.read(chunk_size) |
0 commit comments