Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

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("
" ,'')
       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()
Comment

PREVIOUS NEXT
Code Example
Python :: if elseif in single line python 
Python :: python list.peek 
Python :: calculate days between two dates python 
Python :: numpy arrauy to df 
Python :: python add up values in list 
Python :: colorbar min max matplotlib 
Python :: how to remove numbers from a dataframe in python 
Python :: concat dataframes 
Python :: reportlab page size a4 
Python :: convert list to generator python 
Python :: delete columns pandas 
Python :: jinja macro import 
Python :: file searching in python 
Python :: read from text file and append in list 
Python :: rotate point around point python 
Python :: copyfile pyhon 
Python :: pandas drop column in dataframe 
Python :: convert float in datetime python 
Python :: remove extra spaces and empty lines from string python 
Python :: how to make python turn a list into a text file grapper 
Python :: python code to receive gmail 
Python :: python multiline string 
Python :: operator precedence in python 
Python :: discordpy get role by id 
Python :: how to append leading zeros in python 
Python :: python iterate set 
Python :: python pip jupyter notebook install 
Python :: removing whitespaces from pandas dataframe csv 
Python :: venv 
Python :: python logging into two different files 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =