I've added the following line to a python file in order to output all print strings to a file:
sys.stdout = open(logf, 'wb')
This does what I wanted, but also stops all print strings from appearing in the terminal. Is there a way to configure stdout to print to both destinations? (I'm trying to avoid writing a function for each use of print because there are many and it's a lot to rewrite. Also, I've perused several similar topics, but I haven't found a simple method.)
def fprint(output):
print output
with open("somefile.txt", "a") as f:
f.write("{}\n".format(output))
This function would do it for you, use instead of print.
Related
I have a script originally written for python 3.5+. I am needing to convert it to 2.6.2.
In the script I utilize readlines() which I believe behaves differently between the two versions.
Specifically I am using the readlines() to retrieve data from a txt file that is separated by line breaks.
Here is a snippit:
t=open(supportID+"stmp.txt","r")
timeIn=(t.readlines())
a=(str(timeIn))
supportID=(t.readlines())
b=(str(supportID))
branch=(t.readlines())
c=(str(branch))
clientID=(t.readlines())
d=(str(clientID))
problem=(t.readlines())
e=(str(problem))
solution=(t.readlines())
f=(str(solution))
timeOut=(t.readlines())
g=(str(timeOut))
In my script for 3.x I had a '1' in each of the readlines() and it performed as needed, however with 2.x this does not work. I have tried entering values 1-7 and blank as seen above.
With some research I discovered that some 2.x users use with open(filename)
Is this the preferred method or is there a way to alter my original to make it work?
EDIT:
So im going to use this format
with open(supportID+"stmp.txt") as t:
for line in t:
print(line)
I plugged this in and it works, printing each line as a line in my shell. Now I will want to use this to assign each line to a variable.
EDIT 2:
This is currently working for my environment but is not best practice for this function. Reads in each line and assigns each line to a variable.
t=open(supportID+"stmp.txt","r")
lines = t.readlines()
t.close()
a=lines[0]
b=lines[1]
c=lines[2]
...
To read in each line from the file and assign to a variable I used this method for python 2.6.2
t=open(supportID+"stmp.txt","r")
lines = t.readlines()
t.close()
a=lines[0]
b=lines[1]
c=lines[2]
...
try this code: (I assume it will work with python version >= 2.6)
with open('lines.txt','r') as f:
lines = f.readlines()
for line in lines:
print(line.strip())
Please do not behead me for my noob question. I have looked up many other questions on stackoverflow concerning this topic, but haven't found a solution that works as intended.
The Problem:
I have a fairly large txt-file (about 5 MB) that I want to copy via readlines() or any other build in string-handling function into a new file. For smaller files the following code sure works (only schematically coded here):
f = open('C:/.../old.txt', 'r');
n = open('C:/.../new.txt', 'w');
for line in f:
print(line, file=n);
However, as I found out here (UnicodeDecodeError: 'charmap' codec can't encode character X at position Y: character maps to undefined), internal restrictions of Windows prohibit this from working on larger files. So far, the only solution I came up with is the following:
f = open('C:/.../old.txt', 'r', encoding='utf8', errors='ignore');
n = open('C:/.../new.txt', 'a');
for line in f:
print(line, file=sys.stderr) and append(line, file='C:/.../new.txt');
f.close();
n.close();
But this doesn't work. I do get a new.txt-file, but it is empty. So, how do I iterate through a long txt-file and write every line into a new txt-file? Is there a way to read the sys.stderr as the source for the new file (I actually don't have any idea, what this sys.stderr is)?
I know this is a noob question, but I don't know where to look for an answer anymore.
Thanks in advance!
There is no need to use print() just write() to the file:
with open('C:/.../old.txt', 'r') as f, open('C:/.../new.txt', 'w') as n:
n.writelines(f)
However, it sounds like you may have an encoding issue, so make sure that both files are opened with the correct encoding. If you provide the error output perhaps more help can be provided.
BTW: Python doesn't use ; as a line terminator, it can be used to separate 2 statements if you want to put them on the same line but this is generally considered bad form.
You can set standard output to file like my code.
I successfully copied 6MB text file with this.
import sys
bigoutput = open("bigcopy.txt", "w")
sys.stdout = bigoutput
with open("big.txt", "r") as biginput:
for bigline in biginput.readlines():
print(bigline.replace("\n", ""))
bigoutput.close()
Why don't you just use the shutil module and copy the file?
you can try with this code it works for me.
with open("file_path/../large_file.txt") as f:
with open("file_path/../new_file", "wb") as new_f:
new_f.writelines(f.readlines())
new_f.close()
f.close()
I am new to Python and I have programmed a very basic program which opens a pre-made file reads it, then closes it, through normal Python syntax:
f = open("file", "r+")
f.read()
f.close()
However, once run, this program produces no output.
Is there a problem with my syntax, or is there an error in my PyCharm installation?
This is supposed to happen. f.read() does not print things to the screen. It returns them, just like open() returns f. If you want to print things, you need to call the print() function (under Python 3) or use the print statement (under Python 2).
I'm really frustrated with this strange behaviour of Python all of a sudden. I've been writing to files all sorts of data but since today morning it just doesn't seem to work. I've referred to all these before posting:
How to redirect 'print' output to a file using python?
Failed to write to file but generates no Error
Unable to write data into a file using python
Unable to write list of elements to a file using python
Have tried all following commands but it just doesn't write anything to the delete.txt file. What's happening?
fl=open('delete.txt','w')
fl.write(msg) <--Doesnt work,tried this also
fl.write('%s' %msg) <--Doesnt work,tried this also
fl.write("at least write this") <-- Doesnt work,tried this also
print (msg) <- WORKS
Code:
for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')
try:
if len(i)>40:
mes=processComUni(i)
proc=nltk.tokenize.word_tokenize(mes)
#print proc
pos=nltk.pos_tag(proc)
for i in pos:
if ((i[1]=="NN") or (i[1]=="NNP") or (i[1]=="NNS")) and len(i[0])>2:
#print i[0],i[1]
for j in home_depo_inv:
if i[0] in j.split() and (i[0]!='depot' and i[0]!='home' and i[0]!='store' and i[0]!='por' and i[0]!='get' and i[0]!='house' and i[0]!='find' and i[0]!='part' and i[0]!='son' and i[0]!='put' and i[0]!='lot' and i[0]!='christmas' and i[0]!='post'):
a=re.findall(i[0],j)
fl.write(str(i))<--Doesnt work,tried this also
fl.write(str(mes))<--Doesnt work,tried this also
fl.write("\n")<--Doesnt work,tried this also
fl.write("hello")<--Doesnt work,tried this also
fl.flush()
break
except:
continue
fl.close()
More code:
type(mes) = str
mes="omg would love front yard"
Your snippet's indentation is totally messed up, but anyway: your code starts with:
for i in hd_com.comment_message[1:500]:
fl=open('delete.txt','wb')
which means you reopen the file for writing on each iteration, erasing whatever might have been written by the previous iteration.
You need to flush the output stream explicitly when writing to a file handle like that.
f = open("test.txt", "w")
f.write("this is a test\n")
# no text in the output file at this point
f.flush()
# buffers are flushed to the file
f.write("this, too, is a test\n")
# again, line doesn't show in the file
f.close()
# closing the file flushes the buffers, text appears in file
From the documentation of file.write:
Note that due to buffering, flush() or close() may be needed before the file on disk reflects the data written.
I am creating a Python script to collect data on underlying hardware from cat /proc/cpuinfo
I am trying to extract information i need. But I am having a problem. Here is the script
import os
p=os.popen ("cat /proc/cpuinfo")
string=[]
i=0
for line in p.readlines():
string.append(line.split(":"))
if(string[i][0]=='model name'):
fout = open("information.txt", "w")
fout.write("processor:")
fout.write(string[i][1])
fout.close()
i+=1
My program does not enter if loop at all why? Thanks in advance for help
There is no point to use cat at all here. Refactor it like this:
with open("/proc/cpuinfo") as f:
for line in f:
# potato potato ...
it probably does enter the loop but there might be a whitespace around "model name". You could call .strip() to remove it.
You can open /proc/cpuinfo as a file:
with open("/proc/cpuinfo") as file:
for line in file:
key, sep, value = line.partition(":")
if sep and key.strip() == "model name":
with open("information.txt", "w") as outfile:
outfile.write("processor:" + value.strip())
break
Hard to say what exactly is wrong. I could not figure that out at a glance, though on my Ubuntu 12.10 it also fails in the same way. Anyway, use the subprocess module since popen is deprecated.
subprocess.check_output(['cat', '/proc/cpuinfo']) returns a string quite successfully, at least on my system. And subprocess.check_output(['cat', '/proc/cpuinfo']).split('\n') will give you a list you may iterate through.
Also note that string[i][0]=='model name' won't work. There are tabs after splitting that line by ':'. Do not forget to call strip(): string[i][0].strip()=='model name'
Then, on Python 2.6+ (or even 2.5+, though 2.5 requires from __future__ import with_statement) it's almost always a good practice to use with for dealing with a file you need to open:
with open("information.txt", "w") as fout:
fout.write("processor:")
fout.write(string[i][1])
And finally, those saying you may just open a file and read it, are quite right. That is the best solution:
with open('/proc/cpuinfo') as f:
#Here you may read the file directly.
You could try doing it as :
for line in p.readlines():
line=line.split(":")
if(line[0]=='model name\t') :
#Do work
If you dont need the complete list string.