python write to file
file = open(“testfile.txt”,”w”)
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
file.close()
python write to file
file = open(“testfile.txt”,”w”)
file.write(“Hello World”)
file.write(“This is our new text file”)
file.write(“and this is another line.”)
file.write(“Why? Because we can.”)
file.close()
python write to file
with open(filename,"w") as f:
f.write('Hello World')
python write to file
with open("test.txt",'w',encoding = 'utf-8') as f:
f.write("my first filen")
f.write("This filenn")
f.write("contains three linesn")
python file reading
fin = open("NAME.txt", 'r')
body = fin.read().split("n")
line = fin.readline().strip()
python open and read file with
with open('pagehead.section.htm','r') as f:
output = f.read()
read files and write into another files python
import sys
import glob
import os.path
list_of_files = glob.glob('/Users/Emily/Topics/*.txt') #500 files
for file_name in list_of_files:
print(file_name)
# This needs to be done *inside the loop*
f= open(file_name, 'r')
lst = []
for line in f:
line.strip()
line = line.replace("n" ,'')
line = line.replace("//" , '')
lst.append(line)
f.close()
f=open(os.path.join('/Users/Emily/UpdatedTopics',
os.path.basename(file_name)) , 'w')
for line in lst:
f.write(line)
f.close()
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us