Could someone please explain why this code won't write to a text file?
I run the code but a text file is not created and also cannot be opened using f.open("data.txt","r")
f = open("data.txt", "w+")
n = 1
s = 0
for n in range(1, 999999):
s += 1/n**2
print(s, end="\r")
x = s*6
pisquare = math.sqrt(x)
f.write("Pi is ", pisquare)
f.close()
The recommended way to open and write to a file is as follow:
with open(file_path, 'w') as file:
file.write(f'Pi is {pisquare}')
We use the context manager with so the file closes automatically when done with .write(). This prevents memory corruption when your program exits prematurely I believe.
However, as you have probably noticed, your problem comes from this line:
f.write("Pi is ", pisquare)
You are giving .write() two arguments rather than one string.
import math
f = open("data.txt", "w+")
n = 1
s = 0
for n in range(1, 999999):
s += 1/n**2
print(s, end="\r")
x = s*6
pisquare = math.sqrt(x)
f.write("Pi is " + str(pisquare))
f.close()
I am able to create the text file. Please check for it in your current directory. But if I understand your code correctly, this is what you are looking for -
import math
n = 1
s = 0
with open("data.txt", "w+") as f:
for n in range(1, 9999):
s += 1/n**2
print(s, end="\r")
x = s*6
pisquare = math.sqrt(x)
f.write(" Pi is " + str(pisquare) + "\n")
Related
I'm trying to change multiple variables in another file, but only one variable will get updated unless I close out and reenter the program. This is a simple version of the code:
import pyVars
def loop():
newVar = int(input('New var: '))
temp1 = newVar
temp2 = pyVars.varA
temp3 = pyVars.varB
f = open('pyVars.py', 'w')
f.close()
f = open('pyVars.py', 'a')
f.write('varA = ' + str(temp1) + '\n')
f.write('varB = ' + str(temp2) + '\n')
f.write('varC = ' + str(temp3) + '\n')
f.close()
f = open('pyVars.py')
print(f.read())
f.close()
loop()
loop()
And I get the variables from another file I created in the same folder:
varA = 0
varB = 0
varC = 0
Only varA keeps changing. How do I fix this?
You can use Python's clever variable swapping syntax as follows:
import pyVars
def loop():
newVar = int(input('New var: '))
pyVars.varA, pyVars.varB, pyVars.varC = newVar, pyVars.varA, pyVars.varB
with open('pyVars.py', 'rb') as f:
print(f.read())
loop()
loop()
This most probably has to do with the import statement. When you write:
import pyVars
You get varA=0, varB=0, varC=0 as copies of the pyVars variables.
No matter how many times you change the values of varA, varB, varC, whenever you try to read pyVars.varA, pyVars.varB, it is always returning 0 as that was the vaues it had imported. I initally set varA=700, varB=800, varC=900 and debugged, and on each loop, the initial values of varA was still 700. I checked the initial values by making changes to the code:
from pyVar import varA, varB, varC
While debugging, varA always returned 700, even though the actual value of varA was different in pyVars.py file.
That is also the reason why only varA is getting updated on your pyVars.py file. A is getting the input from the user, but B and C were set to the values of A and B, which were imported as 0. So they always stay as 0.
This is my understanding after fiddling with the code for more than 30 mins now. I would be happy to be corrected by someone, but nevertheless, it has been a great question/learning experience for me. Thank you!
Edit: So I have finally got it to work. It was related to the import statement. Since you are changing the source file that was imported, you will need to reload the file if you need to use the new values. Please see the code that I have changed, which works for me:
import importlib
def loop():
import pyVars
pyVars = importlib.reload(pyVars)
newVar = int(input('New var: '))
temp1 = newVar
temp2 = pyVars.varA
temp3 = pyVars.varB
f = open('pyVars.py', 'w')
f.close()
f = open('pyVars.py', 'a')
f.write('varA = ' + str(temp1) + '\n')
f.write('varB = ' + str(temp2) + '\n')
f.write('varC = ' + str(temp3) + '\n')
f.close()
f = open('pyVars.py')
print(f.read())
f.close()
loop()
loop()
I have 50 files in a directory that are suppose to compare with one file, e.g., original.txt. I have the following code. It works well when I give the file name one-by-one, manually. I want to automate it for this I used 'glob.blog'
folder = "files/"
path = '*.rbd'
path = folder + path
files=sorted(glob.glob(path))
Here the complete code:
import glob
from itertools import islice
import linecache
num_lines_nonbram = 1891427
bits_perline = 32
total_bit_flips = 0
num_bit_diff_flip_zero = 0
num_bit_diff_flip_ones = 0
folder = "files/"
path = '*.rbd'
path = folder + path
files=sorted(glob.glob(path))
original=open('files/mull-original-readback.rbd','r')
#source1 = open(file1, "r")
for filename in files:
del_lines = 101
with open(filename,'r') as f:
i=1
while i <= del_lines:
line1 = f.readline()
lineoriginal=original.readline()
i+=1
i=0
num_bit_diff_flip_zero = 0
num_bit_diff_flip_ones = 0
num_lines_diff =0
i=0
j=0
k=0
a_write2 = ""
while i < (num_lines_nonbram-del_lines):
line1 = f.readline()
lineoriginal = original.readline()
while k < bits_perline:
if ((lineoriginal[k] == line1[k])):
a_write2 += " "
else:
if (lineoriginal[k]=="0"):
#if ((line1[k]=="0" and line1[k]=="1")):
num_bit_diff_flip_zero += 1
if (lineoriginal[k]=="1"):
#if ((line1[k]=="0" and line1[k]=="1")):
num_bit_diff_flip_ones += 1
#if ((line1[k]==1 and line1[k]==0)):
#a_write_file2 = str(i+1) + " " + str(31-k) + "\n" + a_write_file2
#a_write2 += "^"
#num_bit_diff_flip_one += 1
# else:
# a_write2 += " "
k+=1
total_bit_flips=num_bit_diff_flip_zero+num_bit_diff_flip_ones
i+=1
k=0
i = 0
print files
print "Number of bits flip zero= %d" %num_bit_diff_flip_zero +"\n" +"Number of bits flip one= %d" %num_bit_diff_flip_ones +"\n" "Total bit flips = %d " %total_bit_flips
f.close()
original.close()
I got the error:
Traceback (most recent call last):
File "random-ones-zeros.py", line 65, in <module>
if ((lineoriginal[k] == line1[k])):
IndexError: string index out of range
I guess there is some issue with the reading the file automatically, instead giving name manually. But, didn't able to find the solution.
For this the string index is out of range because the value k is iterated once more than intended so the value of the variable exceeds the scope of the program. This should be able to be fixed by using substituting it to
if ((lineoriginal[k-1] == line1[k-1])):
Hope this helps, but I can't access Python right now so I can't test it out :-)
I have a problem with writing to file.
What seems to happen is that a program prints numbers to screen in the order 0,1,2,3 (zeroth, first, second, third) but writes to file in the order -1, 0, 1, 2. Even when the print to screen command follows the write to file command.
Sample code follows. Any ideas how to make it write to file in order 0,1,2,3?
Many thanks - Scriptham.
import random
import time
ln = 4
mins = 10
j = 0
n_sensor = 0
temp_c = 0
data_file = "/home/pi/laboratory/test.csv"
def read_temp():
temp_c = 100 * random.random()
return str("%.3f"% temp_c)
for j in range (1,mins):
f = open(data_file,'a')
f.write("\n" + str(j))
f.close
for n_sensor in range (0,ln):
#device_file_1 =
print("A " + str(n_sensor))
x = read_temp()
f = open(data_file, 'a')
f.write("," + x)
f.close
print("OP temp_c = ", x)
#time.sleep(0.5)
time.sleep(10) #normally would be 59.5 or 60 or similar
quit()
The problem is most likely that you're opening the output file dozens of times, but never closing it.
You should do f = open(data_file,'a') before the loop and only once. Then when everything's done, call f.close() (f.close is not the same as f.close()!).
To make sure the file is always closed, you should use the with statement.
For example:
with open(data_file, 'a') as f:
f.write("\n" + str(j))
This will close the file, even if an exception happens during the write.
Alternatively, you need to use something like:
f = open(data_file, 'a')
try:
f.write("\n" + str(j))
finally:
f.close()
Current program:
#!/usr/bin/python
lookup = 'Loop time'
with open('log.lammps') as myFile:
found = []
for num, line in enumerate(myFile, 1):
if lookup in line:
print 'found at line:', num
found.append(num)
print found
a = int(found[0])
b = int(found[1])
c = int(found[2])
d = int(found[3])
lookup = 'Memory usage per processor ='
with open('log.lammps') as myFile:
found2 = []
for num, line in enumerate(myFile, 1):
if lookup in line:
print 'found at line:', num
found2.append(num)
print found2
e = int(found2[0])
f = int(found2[1])
g = int(found2[2])
h = int(found2[3])
x = 0
while x < len(found):
a = found[x]
e = found2[x]
print "Some thing useful!"
for w,line in enumerate(open("log.lammps")):
if w >= e and w < a :
print line,
x += 1
How do i make it store the last while function into a document. I want to it make a file with the data that this program prints out so that I can have matlab read that file.
The easiest solution is to redirect the output to a file. Let script.py be your python script:
./script.py > out.txt
Otherwise you can redirect the stdout to a file directly in python.
#!/usr/bin/python
import sys
sys.stdout = open('out.txt', 'w')
lookup = 'Loop time'
...
Now every print statement will be redirected to out.txt
One very fast way is to replace sys.stdout with an open file descriptor.
f = open("file.txt","w")
old_stdout = sys.stdout
sys.stdout = f
while x < len(found):
a = found[x]
e = found2[x]
print "Some thing useful!"
for w,line in enumerate(open("log.lammps")):
if w >= e and w < a :
print line,
x += 1
sys.stdout = old_stdout
f.close()
This will automatically redirect anything you print to file.txt instead of stdout. It's also a little hackish, though. It would be a better idea just to open the write file and then replace print ... with f.write(...) wherever you see it, since you don't have any functions that do complex printing.
(I'm assuming you only want the last while loop written to a file, as your question stated.)
Open a file at the start and write the data as you go:
with open('output.txt',"w") as out:
Wherever you print data just write it to a file also:
print 'found at line: {}'.format(num)
out.write('found at line: {}\n'.format(num)) etc..
Just start your code like:
with open('output.txt',"w") as out:
with open('log.lammps') as myFile:
# continue with your code
The important thing about sys.stdout is that you have to import sys instead of from sys import stdout
i.e. this won't redirect your output to a file:
>>> from sys import stdout
>>> stdout = open('/dev/null', 'w')
>>> print 1
1 # not working
Essentially what I am attempting to do is read 'n' number of lines from a file and then write them to a separate file. This program essentially should take a file that has 100 lines and separate that file into 50 separate files.
def main():
from itertools import islice
userfile = raw_input("Please enter the file you wish to open\n(must be in this directory): ")
file1 = open(userfile, "r+")
#print "Name: ", file1.name
#print "Closed or not", file1.closed
#print "Opening mode: ", file1.mode
#print "Softspace flag: ", file1.softspace
jcardtop = file1.read(221);
#print jcardtop
n = 2
count = 0
while True:
next_n_lines = list(islice(file1,n))
print next_n_lines
count = count + 1
fileout = open(str(count)+ ".txt", "w+")
fileout.write(str(jcardtop))
fileout.write(str(next_n_lines))
fileout.close()
break
if not next_n_lines:
break
I do have the file printing as well to show what is in the variable next_n_lines.
*['\n', "randomtext' more junk here\n"]
I would like it instead to look like
randomtext' more junk here
Is this a limitatoin of the islice function? Or am I missing a portion of the syntax?
Thanks for your time!
Where you call str() or print, you want to ''.join(next_n_lines) instead:
print ''.join(next_n_lines)
and
fileout.write(''.join(next_n_lines))
You can store the flattened string in a variable if you don't want to call join twice.
Did you mean something like this?
f = open(userfile,"r")
start = 4
n_lines = 100
for line in f.readlines()[start:(start + n_lines)]:
print line
#do stuff with line
or maybe this rough, yet effective code:
f = open(userfile,"r")
start = 4
end = start + 100
count = start
while count != end:
for line in f.readlines()[count:(count + 2)]:
fileout = open(str(count)+ ".txt", "w+")
fileout.write(str(line))
fileout.close()
count = count + 2