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
Post a Comment