file python 3 code example
Example 1: python write to file
with open(filename,"w") as f:
f.write('Hello World')
Example 2: python with file.open
file = open("welcome.txt", "r")
data = file.read()
file.close()
with open("welcome.txt") as infile:
data = file.read()
Example 3: python file open
file = open('C:\Users\yourname\files\file.txt','r')
text = file.read()
file = open('C:\Users\yourname\files\file.txt','w')
file.write('This is a typical string')
file.close()
Example 4: python write to file
with open("testfile.txt", "w") as f:
f.write("Hello World")
Example 5: with open as file python
>>> with open('workfile') as f:
... read_data = f.read()
>>>
>>> f.closed
True