not able to replace string in a file using replace method - python

Not able to replace the string in file
with open("dc_setup.tcl",'r+') as file:
for line in file:
if str0 in line:
line1=line
print(line1)
contents=file.read()
contents=contents.replace(line1,new_str)
file.seek(0)
file.truncate()
file.write(contents)
I expect the code to replace string in that file , but I'm getting empty file

This section:
file.seek(0)
file.truncate()
file.write(contents)
Is overwriting the entire file, not just your current line. Editing text files in place is generally pretty hard, so the usual approach is to write to a new file. You can copy the new file back over the old file once you've finished if you like.
with open("dc_setup.tcl") as infile, open("new_dc_setup.tcl", "w") as outfile:
for line in infile:
if old_str in line:
line = line.replace(old_str, new_str)
outfile.write(line)

Related

How to split a text file and store each line into a separate file? [duplicate]

This question already has answers here:
for each line in file write line to an individual file in python
(3 answers)
Closed 1 year ago.
I have a text file containing one string per line and I want to split each line of this text file and that each line should be stored as a separate text file.
Could you please help me out with this?
with open('filename.txt') as file:
lines = file.readlines()
# use an identifier from each line to ensure new file name is unique
line_num = 0
for line in lines:
try:
with open('line_{0}.txt'.format(line_num), 'w') as new_file:
new_file.write(line)
# increment the identifier
line_num+=1
except ():
print('Error occured')
This would work:
with open('filename') as file:
for line in file:
with open("change file name every time in this loop", "x") as newFile:
newFile.write(line)

How I can simulateneously sort and unique files from directory using python?

I am trying to sort and unique 30 files of different sizes in one single file.
Each file contains a single line and are separated by newline. That means the file has simple text on each single line.
Here is what I tried to attempt:
lines_seen = set() # holds lines already seen
outfile = open('out.txt', "w")
for line in open('d:\\testing\\*', "r"):
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()
The folder name is testing and it contains 30 different files, which I am trying to combine into file out.txt. The output will be the sorted and unique text, written on each line of the output file.
Well, I thought it would be easy, if I write d:\\testing\\* and it will read the files from the folder. But I got error:
Traceback (most recent call last):
File "sort and unique.py", line 3, in <module>
for line in open('d:\\testing\\*', "r"):
OSError: [Errno 22] Invalid argument: 'd:\\testing\\*'
I would like to know how I can get rid of this error and process my all files efficiently into one single output without any unsuccess.
Please note: RAM is 8 GB and the folder size is about 10 GB.
You just need to loop over all files using os.listdir. Something like this:
lines_seen = set() # holds lines already seen
outfile = open('out.txt', "w")
path = r'd:\testing'
for file in os.listdir(path): #added this line
current_file = os.path.join(path, file)
for line in open(current_file, "r"):
if line not in lines_seen: # not a duplicate
outfile.write(line)
lines_seen.add(line)
outfile.close()

Creating text files, appending them to zip, then delete them

I am trying to get the code below to read the file raw.txt, split it by lines and save every individual line as a .txt file. I then want to append every text file to splits.zip, and delete them after appending so that the only thing remaining when the process is done is the splits.zip, which can then be moved elsewhere to be unzipped. With the current code, I get the following error:
Traceback (most recent call last): File "/Users/Simon/PycharmProjects/text-tools/file-splitter-txt.p‌​y",
line 13, in <module> at stonehenge summoning the all father. z.write(new_file)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framewo‌​rk/Versions/2.7/lib/‌​python2.7/zipfile.py‌​", line 1123, in write st = os.stat(filename) TypeError: coercing to Unicode: need string or buffer,
file found
My code:
import zipfile
import os
z = zipfile.ZipFile("splits.zip", "w")
count = 0
with open('raw.txt','r') as infile:
for line in infile:
print line
count +=1
with open(str(count) + '.txt','w') as new_file:
new_file.write(str(line))
z.write(new_file)
os.remove(new_file)
You could simply use writestr to write a string directly into the zipFile. For example:
zf.writestr(str(count) + '.txt', str(line), compress_type=...)
Use the file name like below. write method expects the filename and remove expects path. But you have given the file (file_name)
z.write(str(count) + '.txt')
os.remove(str(count) + '.txt')

unable to read large bz2 file

I am trying to read a large bz2 file with this code:
import bz2
file= bz2.BZ2File("20150219.csv.bz2","rb")
print file.read()
file.close()
But after 4525 lines, it stops without an error message. The bz2 file is much bigger.
How can I read the whole file line by line?
Your file.read() call tries to read the entire file into memory and then and decompress all of it there, too. Try reading it a line at a time:
import bz2
with bz2.BZ2File("20150219.csv.bz2", "r") as file:
for line in file:
print(line)
Why do you want to print a binary file line by line? Read them to a bytes object instead:
bs = file.read()

Getting next line in a file

I am reading in a file and wonder if there's a way to read the next line in a for loop?
I am currently reading the file like this:
file = open(input,"r").read()
for line in file.splitlines():
line = doSomething()
So is there anyway I can retrieve the next line of the file in that for loop such that I can perform some operation in the doSomething() function?
Just loop over the open file:
infile = open(input,"r")
for line in infile:
line = doSomething(line, next(infile))
Because you now use the file as an iterator, you can call the next() function on the infile variable at any time to retrieve an extra line.
Two extra tips:
Don't call your variable file; it masks the built-in file type object in python. I named it infile instead.
You can use the open file as a context manager with the with statement. It'll close the file for you automatically when done:
with open(input,"r") as infile:
for line in infile:
line = doSomething(line, next(infile))
file = open(input,"r").read()
lines = file.read().splitlines()
for i in range(len(lines)):
line = lines[i]
next_line = lines[i+1]
I think that you mean that if you are in line n, you want to be able to access line n+1.
The simplest way to do that is to replace
for line in file.splitlines():
with
lines = file.readlines()
for i in xrange(len(lines)):
then you can get the current line with lines[i] and the next line with lines[i+1]
the more pythonic way is to use enumerate
lines = file.readlines()
for index, line in enumerate(lines):
now you have the current line in "line" like normal, but you also have the index if you want to find a different line relative to it.

Categories