Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to External File
#1
I wonder if someone could point out why this script is not writing to a file. I can see the correct values populating the terminal but when I go to open the output file it wrote to, it was empty.

file = open(file_ls, "w")
# first line contains 3 integers: the grid dimension, nx and ny, and the total number of snapshots nt
print("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps), open(file_ls, "a"))
# then it follows by the X coordinates of gridded data (from lines 2 to nx+1), Y coordinates of gridded data (from lines nx+1 to nx+ny+1), and the times at which the snapshots are created (from lines nx+ny+2 to nx+ny+nt+1)
for ii in np.arange(np.shape(kxx)[1]):
    print("%.8f" % (kxx[0, ii]), open(file_ls, "a"))
# for ii in arrange(shape(kyy)[0]):
for ii in range(np.shape(kyy)[0] - 1, -1, -1):
    print("%.8f" % (kyy[ii, 0]), open(file_ls, "a"))
for ii in np.arange(collapse_steps):
    mytime = collapse_times[ii] + collapse_starttime
    print("%.8f" % mytime, open(file_ls, "a"))

# all the nt snapshots of seafloor variation are written into a single column one by one from t=t1 to t=tn; for each snapshot, deltah, the data should be written row by row from the left (i=1) to the right (i=nx) from the bottom (j=1) to the top (j=nx)
# deltaH > 0 corresponds to UPLIFT
# deltaH < 0 corresponds to SUBSIDE

deltaH = elev_t0 - elev_t1
###### time - start of collapse => initial bath => zero difference ??? bit stupid ??? check w/comcot sources
for ii in np.arange(np.shape(elev_t1)[0]):
    for jj in np.arange(np.shape(elev_t1)[1]):
        print("%.8f" % 0.0, open(file_ls, "a"))

for ii in np.arange(np.shape(elev_t1)[0]):
    for jj in np.arange(np.shape(elev_t1)[1]):
        print("%.8f" % (deltaH[ii, jj]), open(file_ls, "a"))

file.close()
buran write Mar-29-2022, 07:36 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Use proper tags please.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
Open the file one time and save the file object that returns in a variable. Then use that variable for writing (or printing).

with open("file_ls", "w") as output: #  'output' stores the file object.  By default file is open for text.
    # can easily print() or write() to the file object.
    output.write("Text data\n") # usually use the write() method on the file object to push data to the file.
    print("More text", file=output) # print() needs an explicit argument to print other than to the console

# No need to close.  Automatically closed when we leave the <with open()> context.
Gribouillis likes this post
Reply
#4
Hi Bowlofred, many thanks, I appreciate it.

Could you correct me again please. I get error below for "output.write" plus my coding for "print("%.8f" % (kyy[ii, 0]), file=output) gives an error on file=output:
Error:
Traceback (most recent call last): File "C:/Users/David/PycharmProjects/pythonProject2/venv/init_collapse.py", line 226, in <module> output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps) TypeError: write() argument must be str, not generator
with open(file_ls, "w") as output:
    output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps)
for ii in np.arange(np.shape(kxx)[1]))
print("%.8f" % (kxx[0, ii]), file=output)
# for ii in arrange(shape(kyy)[0]):
for ii in range(np.shape(kyy)[0] - 1, -1, -1):
    print("%.8f" % (kyy[ii, 0]), file=output)
for ii in np.arange(collapse_steps):
    mytime = collapse_times[ii] + collapse_starttime
    print("%.8f" % mytime, file=output)
buran write Mar-29-2022, 07:38 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#5
Not according to your error message. According to the error message the problem is here:
    output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps)
I am surprised this is not showing up as a syntax error because of the mismatched parenthesis. So I loaded the code into my editor and is says these parenthesis match.
with open(file_ls, "w") as output:
    output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps)  # expected ) here
for ii in np.arange(np.shape(kxx)[1]))  # Python says matching paren for write ( is end of this line
Now the message makes more sense. It sees the for loop inside the parenthesis for the argument list for the write() command and complains about the generator.
Reply
#6
I'm still stuck with this printing error from the script extract below. Help please!!!!

Error:
Traceback (most recent call last): File "C:/Users/David/PycharmProjects/pythonProject2/venv/init_collapse.py", line 228, in <module> print("%.8f" % (kxx[0, ii]), file=output) ValueError: I/O operation on closed file. Process finished with exit code 1
 with open(file_ls, "w") as output:
    output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps))
for ii in np.arange((np.shape(kxx)[1])):
        print("%.8f" % (kxx[0, ii]), file=output)
# for ii in arrange(shape(kyy)[0]):
for ii in range(np.shape(kyy)[0] - 1, -1, -1):
       print("%.8f" % (kyy[ii, 0]), file=output)
for ii in np.arange(collapse_steps):
        print("%.8f" % mytime, file=output)
Reply
#7
When you use the with context manager, everything is closed when you leave the indented context. So you need do all your operations on the file in that context.

with open(file_ls, "w") as output:  # starts context where "output" is the file object
    output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps))
for ii in np.arange((np.shape(kxx)[1])): # context over.  "output" is no longer open
So instead something like:
with open(file_ls, "w") as output:
    output.write("%d %d %d" % (np.shape(kxx)[1], np.shape(kxx)[0], collapse_steps))
    for ii in np.arange((np.shape(kxx)[1])):
            print("%.8f" % (kxx[0, ii]), file=output)
    # for ii in arrange(shape(kyy)[0]):
    for ii in range(np.shape(kyy)[0] - 1, -1, -1):
           print("%.8f" % (kyy[ii, 0]), file=output)
    for ii in np.arange(collapse_steps):
            print("%.8f" % mytime, file=output)
Reply
#8
Awesome, thank you. I just posted another problem though. I am not a progrommer, just trying to get some old script by others working.
Reply
#9
How do I ensure that when adding to the file it starts on a new line?
Reply
#10
Either add a newline before your text or (more commonly) make sure all your other lines end with a newline.

print() automatically puts a newline at the end of whatever it prints unless you override that setting.
write() doesn't automatically add a newline. You need to add them yourself.

print("One line") # defaults to newline added
f.write("Second line\n") # newline present in the string
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,387 Sep-27-2022, 01:38 PM
Last Post: buran
  Writing to json file ebolisa 1 1,012 Jul-17-2022, 04:51 PM
Last Post: deanhystad
  Writing to file ends incorrectly project_science 4 2,704 Jan-06-2021, 06:39 PM
Last Post: bowlofred
  Problems Sorting Data in an External File (.txt) Superlegend21 1 4,338 Dec-27-2020, 10:06 PM
Last Post: Superlegend21
  Writing unit test results into a text file ateestructural 3 4,774 Nov-15-2020, 05:41 PM
Last Post: ateestructural
  Writing to file in a specific folder evapa8f 5 3,446 Nov-13-2020, 10:10 PM
Last Post: deanhystad
  Failure in writing binary text to file Gigux 7 3,826 Jul-04-2020, 08:41 AM
Last Post: Gigux
  writing data to a csv-file apollo 1 2,379 Jul-03-2020, 02:28 PM
Last Post: DeaD_EyE
  Writing to File Issue Flash_Stang 3 2,532 Jun-05-2020, 05:14 AM
Last Post: Gribouillis
  Help! Formatting and Writing to a File bwdu 2 2,419 Apr-19-2020, 09:29 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020