I have a text file (filenames.txt) which contains over 200 file paths:
/home/chethan/purpose1/script1.txt
/home/chethan/purpose2/script2.txt
/home/chethan/purpose3/script3.txt
/home/chethan/purpose4/script4.txt
Out of the multiple lines present in each of these files, each of them contain a line which is a filename like Reference.txt. My objective is to replace .txt in Reference.txt with .csv in every file. As a beginner of Python I referred to several questions in stackoverflow on similar cases and wrote the following code.
My code:
#! /usr/bin/python
#filename modify_hob.py
import fileinput
f = open('/home/chethan/filenames.txt', 'r')
for i in f.readlines():
for line in fileinput.FileInput(i.strip(),inplace=1):
line = line.replace("txt","csv"),
f.close()
f.close()
When I run my code, the contents of txt files (script1, script2..) mentioned above are wiped away, i.e., they won't be having a single line of text inside them! I am puzzled with this behavior and not able to find out a solution.
This should get you going (untested):
#! /usr/bin/python
#filename modify_hob.py
# Open the file with filenames list.
with open('filenames.txt') as list_f:
# Iterate over the lines, each line represents a file name.
for filename in list_f:
# Rewrite its content.
with open(filename) as f:
content = f.read()
with open(filename, 'w') as f:
f.write(content.replace('.txt', '.csv'))
In your code below, f is set to the open file object of filename.txt and
nothing else. That is what you are closing in both the last two lines.
Also, you are not writing anything back to the files, so you can't expect your
changes to be written back to the disk. (Unless the fileinput module does some
dark magic that I'm missing.)
Related
I found this solution from stackoverflow but will i have to add this piece of code every time i write a code to perform read/write operations.
Or is there any long term solution available for this ?
import os
path = "E:\Python\learn"
os.chdir(path)
f = open('text.txt')
data = f.read()
print(data)
f.close()
I think you need a way to read same file multiple times in your python code. Use file.seek() to jump to a specific position in a file. However, think about whether it is really necessary to go through the file again. An Example for file.seek:
with open(name, 'r+') as file:
for line in file:
# Do Something with line
file.seek(0)
for line in file:
# Do Something with line, second time
Incase you want to re-iterate the file second time and don't want to open the file again, you can follow this syntax:
with open(name, 'r+') as file:
for line in file:
# Do Something with line
for line in file:
# Do Something with line, second time
Similar to posting: Replace string in a specific line using python, however results were not forethcomming in my slightly different instance.
I working with python 3 on windows 7. I am attempting to batch edit some files in a directory. They are basically text files with .LIC tag. I'm not sure if that is relevant to my issue here. I am able to read the file into python without issue.
My aim is to replace a specific string on a specific line in this file.
import os
import re
groupname = 'Oldtext'
aliasname = 'Newtext'
with open('filename') as f:
data = f.readlines()
data[1] = re.sub(groupname,aliasname, data[1])
f.writelines(data[1])
print(data[1])
print('done')
When running the above code I get an UnsupportedOperation: not writable. I am having some issue writing the changes back to the file. Based on suggestion of other posts, I edited added the w option to the open('filename', "w") function. This causes all text in the file to be deleted.
Based on suggestion, the r+ option was tried. This leads to successful editing of the file, however, instead of editing the correct line, the edited line is appended to the end of the file, leaving the original intact.
Writing a changed line into the middle of a text file is not going to work unless it's exactly the same length as the original - which is the case in your example, but you've got some obvious placeholder text there so I have no idea if the same is true of your actual application code. Here's an approach that doesn't make any such assumption:
with open('filename', 'r') as f:
data = f.readlines()
data[1] = re.sub(groupname,aliasname, data[1])
with open('filename', 'w') as f:
f.writelines(data)
EDIT: If you really wanted to write only the single line back into the file, you'd need to use f.tell() BEFORE reading the line, to remember its position within the file, and then f.seek() to go back to that position before writing.
Would like to use a text file containing multiple windows path/filename.txt files and feed into a for loop which would then take each path leading to a filename.txt and search it for a word. This would happen for each path to a file in the filename.txt file.
So far this part is functioning:
with open ("filename.txt", "r") as myfile:
data=myfile.read()
print (data)
Printing the data gives me this:
The results of printing out the contents of the variable, "data" looks like:
c:/temp\Txt_folder\3rd_lyr_fldr\3rd_infiles.txt
c:/temp\Txt_folder\3rd_lyr_fldr\3rd_ListFile.txt
c:/temp\Txt_folder\3rd_lyr_fldr\3rd_new_filename1.txt
This part of script ,shown below, does not work. The data shown above is not fed into the for loop (shown below) one line at a time but rather one continuous column or at least
that is the way print(data) shows it on my screen.
for line in data:
if re.search(r"something",line):
print(line)
How can this me accomplished.
Here's something that basically does what you want:
keyword = 'whatever'
with open ('filename.txt', 'rt') as myfile:
for filename in (line.strip() for line in myfile):
with open(filename, 'rt') as file:
for line in file:
if keyword in line:
print(line, end='')
I have multiple .txt files in a source folder of which i have given the path in "src" .I want to search strings which looks like "abcd.aiq" and print them in a file which i named as "fi".
I have written the following code and it doesnt print anything inside the file although it doesnt give any error.
import glob
import re
import os
src = (C:\Auto_TEST\Testing\Automation")
file_array= glob.glob(os.path.join(src,".txt"))
fi= open("aiq_hits.txt","w")
for input_file in file_array:
fo=open(input_file,"r")
line=fo.readline()
for line in fo:
line=r.strip()
x= re.findall('\S*.aiq\S*',line)
line= fo.readline()
for item in x:
fi.write("%s\n" %item)
fo.close()
fi.close()
I suppose this is what you are trying:
import glob
import re
import os.path
src = 'C:/Auto_TEST/Testing/Automation'
file_array = glob.glob(os.path.join(src,'*.txt'))
with open("aiq_hits.txt","w") as out_file:
for input_filename in file_array:
with open(input_filename) as in_file:
for line in in_file:
match = re.findall(r'\S*.aiq\S*', line)
for item in match:
out_file.write("%s\n" %item)
Let me quickly describe the changes I've made:
Opening files directly is not always a good idea. If the script crashes, the opened file object isn't being closed again, which can lead to data loss.
Since PEP 343 Python has the with statement, wich is generally agreed on being a better solution when handling files.
Calling f.readline() multiple times results in the script skipping these lines, because for line in f: reads lines on its own.
Finally, after every matching item you found you've been closing both the input file and the output file, so further reading or writing isn't possible anymore.
Edit: If you might need to tweak your regex, this might be a useful resource.
I am trying to recursively loop through a series of directories (about 3 levels deep). In each directory is a series of text files, I want to replace a line of text with the directory path if the line contains a certain string so for example.
/path/to/text/file/fName.txt
If a line in fName in fName.txt text contains the string 'String1' I want to replace this line with 'some text' + file where file is the last part of the path.
This seems like it should be easy in python but I can't seem to manage it.
Edit: Apologies for a very badly written question, I had to rush off, shouldn't have hit enter.
Here's what I have so far
import os
for dirname, dirs, files in os.walk("~/dir1/dir2"):
print files
for fName in files:
fpath = os.path.join(dirname, fName)
print fpath
f = open(fpath)
for line in f:
#where I'm getting stuck
s = s.replace("old_txt", "new_txt")
#create new file and save output
What I'm getting stuck on is how to replace an entire line based on only a section of the line. For example if the line were
That was a useless question,
I can't seem to make replace to what I want. What I'm trying to do is change the entire line based only on searching for 'useless'. Also, is there a better way of modyfying a single line than re-writing the entire file?
Thanks.
os.walk (look at example) is all you need
parse each file with with open(...) as f:, analyze it, and overwrite it (carefully, after testing) with with open(..., 'w') as f: