Answers for "how to open and read a file in python and write to different file"

1

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()
Posted by: Guest on June-28-2020
0

open and write in a file in python

my_file = open("C:\Users\Python\file.txt", "w")
#Give the path accurately and use \
my_file.write("This is the test text")
my_file.close()

# It is always recommended to close the file after modifying
# to see the output, open the file - file.txt
Posted by: Guest on January-06-2022

Code answers related to "how to open and read a file in python and write to different file"

Python Answers by Framework

Browse Popular Code Answers by Language