Using fileinput to write to a file makes issue - python

for line in fileinput.FileInput("file.txt", inplace=1):
if "success" in line:
print(line)
When I use fileinput, the file 'file.txt' is not released. I could see the issue 'file.txt' still in use.
When I do the above function using normal file operation , no issue is shown
How to fix the issue with fileinput.
I used the below code snippet , but the issue is showing again . The file is not getting closed I guess
f = fileinput.input("file.txt", inplace=1)
for line in f:
if "success" in line:
print(line)
f.close()

In Python 2.7, you have to explicitly call close() on the fileinput instance:
try:
f = fileinput.input("file.txt", inplace=1)
for line in f:
if "success" in line:
print line, end=""
else:
f.close()
Since Python 3.2, the FileInput class can be used as a context manager. See the fileinput documentation for more information.
with fileinput.input(files=('file.txt'), inplace=1) as f:
for line in f:
if "success" in line:
print(line)

Related

How to avoid a memory errors in Python when removing empty lines in a large text file? [duplicate]

For example, we have some file like that:
first line
second line
third line
And in result we have to get:
first line
second line
third line
Use ONLY python
The with statement is excellent for automatically opening and closing files.
with open('myfile','rw') as file:
for line in file:
if not line.isspace():
file.write(line)
import fileinput
for line in fileinput.FileInput("file",inplace=1):
if line.rstrip():
print line
import sys
with open("file.txt") as f:
for line in f:
if not line.isspace():
sys.stdout.write(line)
Another way is
with open("file.txt") as f:
print "".join(line for line in f if not line.isspace())
with open(fname, 'r+') as fd:
lines = fd.readlines()
fd.seek(0)
fd.writelines(line for line in lines if line.strip())
fd.truncate()
I know you asked about Python, but your comment about Win and Linux indicates that you're after cross-platform-ness, and Perl is at least as cross-platform as Python. You can do this easily with one line of Perl on the command line, no scripts necessary: perl -ne 'print if /\S/' foo.txt
(I love Python and prefer it to Perl 99% of the time, but sometimes I really wish I could do command-line scripts with it as you can with the -e switch to Perl!)
That said, the following Python script should work. If you expect to do this often or for big files, it should be optimized with compiling the regular expressions too.
#!/usr/bin/python
import re
file = open('foo.txt', 'r')
for line in file.readlines():
if re.search('\S', line): print line,
file.close()
There are lots of ways to do this, that's just one :)
>>> s = """first line
... second line
...
... third line
... """
>>> print '\n'.join([i for i in s.split('\n') if len(i) > 0])
first line
second line
third line
>>>
You can use below way to delete all blank lines:
with open("new_file","r") as f:
for i in f.readlines():
if not i.strip():
continue
if i:
print i,
We can also write the output to file using below way:
with open("new_file","r") as f, open("outfile.txt","w") as outfile:
for i in f.readlines():
if not i.strip():
continue
if i:
outfile.write(i)
Have you tried something like the program below?
for line in open(filename):
if len(line) > 1 or line != '\n':
print(line, end='')
Explanation: On Linux/Windows based platforms where we have shell installed below solution may work as "os" module will be available and trying with Regex
Solution:
import os
os.system("sed -i \'/^$/d\' file.txt")

Syntax error for "with open(file, 'r+'):"

I have a bunch of .csh in a directory and I want to open them one by one, search for "//" and replace it with "/" with a python script. How do I do that?
I tried:
import os
for file in os.listdir("./"):
if file.endswith(".csh"):
with open(file, 'r+'):
data = file.read().replace("//", "/")
f.write(data)
f.close()
But it gives me:
File "script.py", line 4
with open(file, 'r+'):
^
SyntaxError: invalid syntax
You are using an old version of Python. The with statement was introduced in Python 2.5, where it had to be enabled via
from __future__ import with_statement
It is best to upgrade to 2.7, if you need to stay in the 2.x line, or 3.4.
Note that you also need to change your code according the answer by Avinash Raj, capturing the file object in a variable via as f. file.read() will not work because file continues to be the file name string.
Change your code to,
import os
for file in os.listdir("./"):
if file.endswith(".csh"):
with open(file, 'r+') as f:
data = f.read()
f.seek(0)
with open(file, 'w+') as w:
dat = data.replace("//", "/")
w.write(dat)

Use process substitution as input file to Python twice

Consider the following python script
#test.py
import sys
inputfile=sys.argv[1]
with open(inputfile,'r') as f:
for line in f.readlines():
print line
with open(inputfile,'r') as f:
for line in f.readlines():
print line
Now I want to run test.py on a substituted process, e.g.,
python test.py <( cat file | head -10)
It seems the second f.readlines returns empty. Why is that and is there a way to do it without having to specify two input files?
Why is that.
Process substitution works by creating a named pipe. So all the data consumed at the first open/read loop.
Is there a way to do it without having to specify two input files.
How about buffering the data before using it.
Here is a sample code
import sys
import StringIO
inputfile=sys.argv[1]
buffer = StringIO.StringIO()
# buffering
with open(inputfile, 'r') as f:
buffer.write(f.read())
# use it
buffer.seek(0)
for line in buffer:
print line
# use it again
buffer.seek(0)
for line in buffer:
print line
readlines() will read all available lines from the input at once. This is why the second call returns nothing because there is nothing left to read. You can assign the result of readlines() to a local variable and use it as many times as you want:
import sys
inputfile=sys.argv[1]
with open(inputfile,'r') as f:
lines = f.readlines()
for line in lines:
print line
#use it again
for line in lines:
print line

I'm tailing a file in python for any changes and it is not picking up a change to the file

Here is my script:
def tail(file, delay=0.5):
f = open(file, 'r')
f.seek(0, 2)
while True:
line = f.readline()
print 'line: ' + line
if not line:
time.sleep(delay)
else:
print 'line found!'
When i open the file and add some lines to it, this script is not picking it up. I am doing this on linux.
use open('filename', 'a') instead of open('filename', 'r') for adding lines to the file ... I think you actually want to append to the file rather than reading it.
The code looks fine so there is likely a buffering issue. Try using f.read(100) instead of readline so that you read whatever is available rather than searching for line endings.

How to delete all blank lines in the file with the help of python?

For example, we have some file like that:
first line
second line
third line
And in result we have to get:
first line
second line
third line
Use ONLY python
The with statement is excellent for automatically opening and closing files.
with open('myfile','rw') as file:
for line in file:
if not line.isspace():
file.write(line)
import fileinput
for line in fileinput.FileInput("file",inplace=1):
if line.rstrip():
print line
import sys
with open("file.txt") as f:
for line in f:
if not line.isspace():
sys.stdout.write(line)
Another way is
with open("file.txt") as f:
print "".join(line for line in f if not line.isspace())
with open(fname, 'r+') as fd:
lines = fd.readlines()
fd.seek(0)
fd.writelines(line for line in lines if line.strip())
fd.truncate()
I know you asked about Python, but your comment about Win and Linux indicates that you're after cross-platform-ness, and Perl is at least as cross-platform as Python. You can do this easily with one line of Perl on the command line, no scripts necessary: perl -ne 'print if /\S/' foo.txt
(I love Python and prefer it to Perl 99% of the time, but sometimes I really wish I could do command-line scripts with it as you can with the -e switch to Perl!)
That said, the following Python script should work. If you expect to do this often or for big files, it should be optimized with compiling the regular expressions too.
#!/usr/bin/python
import re
file = open('foo.txt', 'r')
for line in file.readlines():
if re.search('\S', line): print line,
file.close()
There are lots of ways to do this, that's just one :)
>>> s = """first line
... second line
...
... third line
... """
>>> print '\n'.join([i for i in s.split('\n') if len(i) > 0])
first line
second line
third line
>>>
You can use below way to delete all blank lines:
with open("new_file","r") as f:
for i in f.readlines():
if not i.strip():
continue
if i:
print i,
We can also write the output to file using below way:
with open("new_file","r") as f, open("outfile.txt","w") as outfile:
for i in f.readlines():
if not i.strip():
continue
if i:
outfile.write(i)
Have you tried something like the program below?
for line in open(filename):
if len(line) > 1 or line != '\n':
print(line, end='')
Explanation: On Linux/Windows based platforms where we have shell installed below solution may work as "os" module will be available and trying with Regex
Solution:
import os
os.system("sed -i \'/^$/d\' file.txt")

Categories

Resources