How to edit lines of all text files in a directory with python -


i edit , replace lines of .txt files in directory python purpose using following code:

path = '.../dbfiles' filename in os.listdir(path): in os.listdir(path):     if i.endswith(".txt"):          open(i, 'r') f_in:             line in f_in:                line=tweet_to_words(line).encode('utf-8')                  open(i, 'w').write(line) 

where tweet_to_words(line) predefined function edition lines of text file. although not sure if logic of code right!? facing following error:

ioerror: [errno 2] no such file or directory: 'thirdweek.txt'

but 'thirdweek.txt' exist in directory! question see if method using editing lines in file right or not!? , if how can fix error ?

you should add base path when use open:

        open(path + '/' + i, 'r') f_in: 

the same goes for:

               open(path + '/' + i, 'w').write(line) 

Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -