5/08/2020

Python - File Objects - Reading and Writing to Files

** File Objects - Reading and Writing to Files


==========================================

# File Objects

f = open('test.txt', 'r')

print(f.name)

f.close()

---------------------------------

test.txt

---------------------------------

==========================================

f = open('test.txt', 'r')

print(f.mode)

f.close()

---------------------------------

r

---------------------------------

==========================================

with open('test.txt', 'r') as f:
pass

print(f.closed)

---------------------------------

True

---------------------------------

==========================================

with open('test.txt', 'r') as f:
pass

print(f.read())

---------------------------------

 print(f.read())
ValueError: I/O operation on closed file.


---------------------------------

==========================================

with open('test.txt', 'r') as f:
f_contets = f.read()
print(f_contets)



---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

---------------------------------

==========================================

with open('test.txt', 'r') as f:
f_contets = f.readlines()
print(f_contets)


---------------------------------

['1) This is a test file!\n', '2) With multiple lines of data...\n', '3) Third line\n', '4) Fourth line\n', '5) Fifth line\n', '6) Sixth line\n', '7) Seventh line\n', '8) Eighth line\n', '9) Ninth line\n', '10) Tenth line']

---------------------------------

==========================================

with open('test.txt', 'r') as f:
f_contets = f.readline()
print(f_contets)

---------------------------------

1) This is a test file!

---------------------------------

==========================================

with open('test.txt', 'r') as f:
f_contets = f.readline()
print(f_contets)

f_contets = f.readline()
print(f_contets)


---------------------------------

1) This is a test file!

2) With multiple lines of data...

---------------------------------

==========================================

with open('test.txt', 'r') as f:
f_contets = f.readline()
print(f_contets, end = '')

f_contets = f.readline()
print(f_contets, end = '')


---------------------------------

1) This is a test file!
2) With multiple lines of data...

---------------------------------

==========================================

with open('test.txt', 'r') as f:

for line in f:
print(line, end = '')

---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

---------------------------------

==========================================

with open('test.txt', 'r') as f:

f_contents = f.read()
print(f_contents, end = '')

---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

---------------------------------


==========================================

with open('test.txt', 'r') as f:

f_contents = f.read(100)
print(f_contents, end = '')

---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line

---------------------------------

==========================================

with open('test.txt', 'r') as f:

f_contents = f.read(100)
print(f_contents, end = '')


f_contents = f.read(100)
print(f_contents, end = '')


---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

---------------------------------

==========================================

with open('test.txt', 'r') as f:

f_contents = f.read(100)
print(f_contents, end = '')


f_contents = f.read(100)
print(f_contents, end = '')

f_contents = f.read(100)
print(f_contents, end = '')

---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

---------------------------------

==========================================

with open('test.txt', 'r') as f:

size_to_read = 100

f_contents = f.read(size_to_read)

while len(f_contents) > 0:
print(f_contents, end = '')
f_contents = f.read(size_to_read)

---------------------------------

1) This is a test file!
2) With multiple lines of data...
3) Third line
4) Fourth line
5) Fifth line
6) Sixth line
7) Seventh line
8) Eighth line
9) Ninth line
10) Tenth line

---------------------------------

==========================================

with open('test.txt', 'r') as f:

size_to_read = 10

f_contents = f.read(size_to_read)

while len(f_contents) > 0:
print(f_contents, end = '*')
f_contents = f.read(size_to_read)

---------------------------------

1) This is* a test fi*le!
2) Wit*h multiple* lines of *data...
3)* Third lin*e
4) Fourt*h line
5) *Fifth line*
6) Sixth *line
7) Se*venth line*
8) Eighth* line
9) N*inth line
*10) Tenth *line
*

==========================================

with open('test.txt', 'r') as f:

size_to_read = 10

f_contents = f.read(size_to_read)

print(f.tell())


---------------------------------

10

---------------------------------

==========================================

with open('test.txt', 'r') as f:

size_to_read = 10

f_contents = f.read(size_to_read)
print(f_contents, end = '')

f_contents = f.read(size_to_read)
print(f_contents, end = '')

print('\n')
print('-'*80)

print(f.tell())

---------------------------------

1) This is a test fi

--------------------------------------------------------------------------------
20

---------------------------------

==========================================

with open('test.txt', 'r') as f:

size_to_read = 10

f_contents = f.read(size_to_read)
print(f_contents, end = '')

f_contents = f.read(size_to_read)
print(f_contents)

---------------------------------

1) This is a test fi

---------------------------------

==========================================

with open('test.txt', 'r') as f:

size_to_read = 10

f_contents = f.read(size_to_read)
print(f_contents, end = '')

f.seek(0)

f_contents = f.read(size_to_read)
print(f_contents)

---------------------------------

1) This is1) This is

---------------------------------

==========================================

with open('test.txt', 'r') as f:
f.write('Test')

---------------------------------

 f.write('Test')
io.UnsupportedOperation: not writable

---------------------------------

==========================================

with open('test2.txt', 'w') as f:
f.write('Test')

---------------------------------




---------------------------------

==========================================

with open('test2.txt', 'w') as f:
f.write('Test')
f.write('Test')

---------------------------------




---------------------------------

==========================================

with open('test2.txt', 'w') as f:
f.write('Test')
f.seek(0)
f.write('Test')

---------------------------------




---------------------------------

==========================================

with open('test2.txt', 'w') as f:
f.write('Test')
f.seek(0)
f.write('R')

---------------------------------



---------------------------------

==========================================

with open('test.txt', 'r') as rf:
with open('test_copy.txt', 'w') as wf:
for line in rf:
wf.write(line)

---------------------------------




---------------------------------

==========================================

ith open('1.jpg', 'r') as rf:
with open('1_copy.jpg', 'w') as wf:
for line in rf:
wf.write(line)

---------------------------------

  for line in rf:
UnicodeDecodeError: 'cp949' codec can't decode byte 0xff in position 0: illegal multibyte sequence

---------------------------------

==========================================

with open('1.jpg', 'rb') as rf:
with open('1_copy.jpg', 'wb') as wf:
for line in rf:
wf.write(line)

---------------------------------




==========================================

with open('1.jpg', 'rb') as rf:
with open('1_copy.jpg', 'wb') as wf:
chunk_size = 4096
rf_chunk = rf.read(chunk_size)
while len(rf_chunk) > 0:
wf.write(rf_chunk)
rf_chunk = rf.read(chunk_size)

---------------------------------