Reading data into Python - python

I have made a save file using
def Save():
savefile = open('save.txt','w')
savefile.write(str(currentLocation)+'\n')
savefile.close()
print("GAME SAVED!", file=sys.stderr)
which works fine but when I go to load it using...
def Load():
savefile = open('save.txt', 'r')
for line in savefile:
currentLocation.append(currentLocation)
savefile.close()
I get an error called :
AttributeError: 'int' object has no attribute 'append'.
Any reason you can think of why this doesn't work?

You are trying to append to a non-list type object:
currentLocation is not a list
If your file only contains one line (with the number to load), then you can read the file and strip the contents to get the number without new lines, spaces, etc.
def Load():
with open('save.txt', 'r') as loadfile:
currentLocation = int(loadfile.read().strip())
The above with statement will automatically close the file after the nested block of code.
There is also int casting to convert the read number from string to int.

Related

Reading a binary file Python (pickle) [duplicate]

I created some data and stored it several times like this:
with open('filename', 'a') as f:
pickle.dump(data, f)
Every time the size of file increased, but when I open file
with open('filename', 'rb') as f:
x = pickle.load(f)
I can see only data from the last time.
How can I correctly read file?
Pickle serializes a single object at a time, and reads back a single object -
the pickled data is recorded in sequence on the file.
If you simply do pickle.load you should be reading the first object serialized into the file (not the last one as you've written).
After unserializing the first object, the file-pointer is at the beggining
of the next object - if you simply call pickle.load again, it will read that next object - do that until the end of the file.
objects = []
with (open("myfile", "rb")) as openfile:
while True:
try:
objects.append(pickle.load(openfile))
except EOFError:
break
There is a read_pickle function as part of pandas 0.22+
import pandas as pd
obj = pd.read_pickle(r'filepath')
The following is an example of how you might write and read a pickle file. Note that if you keep appending pickle data to the file, you will need to continue reading from the file until you find what you want or an exception is generated by reaching the end of the file. That is what the last function does.
import os
import pickle
PICKLE_FILE = 'pickle.dat'
def main():
# append data to the pickle file
add_to_pickle(PICKLE_FILE, 123)
add_to_pickle(PICKLE_FILE, 'Hello')
add_to_pickle(PICKLE_FILE, None)
add_to_pickle(PICKLE_FILE, b'World')
add_to_pickle(PICKLE_FILE, 456.789)
# load & show all stored objects
for item in read_from_pickle(PICKLE_FILE):
print(repr(item))
os.remove(PICKLE_FILE)
def add_to_pickle(path, item):
with open(path, 'ab') as file:
pickle.dump(item, file, pickle.HIGHEST_PROTOCOL)
def read_from_pickle(path):
with open(path, 'rb') as file:
try:
while True:
yield pickle.load(file)
except EOFError:
pass
if __name__ == '__main__':
main()
I developed a software tool that opens (most) Pickle files directly in your browser (nothing is transferred so it's 100% private):
https://pickleviewer.com/ (formerly)
Now it's hosted here: https://fire-6dcaa-273213.web.app/
Edit: Available here if you want to host it somewhere: https://github.com/ch-hristov/Pickle-viewer
Feel free to host this somewhere.

File handling with functions?

So I got this code that is supposed to sort a dictionary within a json file alphabetically by key:
import json
def values(infile,outfile):
with open(infile):
data=json.load(infile)
data=sorted(data)
with open(outfile,"w"):
json.dump(outfile,data)
values("values.json","values_out.json")
And when I run it I get this error:
AttributeError: 'str' object has no attribute 'read'
I'm pretty sure I messed something up when I made the function but I don't know what.
EDIT: This is what the json file contains:
{"two": 2,"one": 1,"three": 3}
You are using the strings infile and outfile in your json calls, you need to use the file description instance, that you get using as keyword
def values(infile,outfile):
with open(infile) as fic_in:
data = json.load(fic_in)
data = sorted(data)
with open(outfile,"w") as fic_out:
json.dump(data, fic_out)
You can group, with statements
def values(infile, outfile):
with open(infile) as fic_in, open(outfile, "w") as fic_out:
json.dump(sorted(json.load(fic_in)), fic_out)
You forgot to assign the file you opened to a variable. In your current code you open a file, but then try to load the filename rather than the actual file. This code should run because you assign the file object reference to my_file.
import json
def values(infile,outfile):
with open(infile) as my_file:
data=json.load(my_file)
data=sorted(data)
with open(outfile,"w"):
json.dump(outfile,data)
values("values.json","values_out.json")

Get AttributeError when copying string from a file into a new file

I am writing a simple program that reads a file, copies its contents and writes the copied content into a new file. I thought I had done it correctly, because when I open "copyFile" the copied contents of the original file is written there as a string. I've written:
copy = open('TestFile').read() #Open 'TestFile', read it into variable
print("Copy of textfile:\t", copy)
copyFile = open('copyText.txt', 'w').write(copy) #Create new file, write in the copied text
copyText = copyFile.read()
print("New file :\t", copyText)
And I am able to print the contents of the file, but when I try to print the copy, i get this error:
Traceback (most recent call last):
File "PATH/TO/THE/FILE/CALLED/copyText.py", line 14, in <module>
copyText = copyFile.read()
AttributeError: 'int' object has no attribute 'read'
The file only has one sentence in it, so I don't understand the error i'm getting.
File write do not return io object. It returns length of the text written.
Also i suggest you should use with statement to write and read from file.
The following code is the right way to do it for your case.
copy = open('TestFile').read() #Open 'TestFile', read it into variable
print("Copy of textfile:\t", copy)
length = open('copyText.txt', 'w').write(copy) #Create new file, write in the copied text
copyText = open('copyText.txt', 'r').read()
print("New file :\t", copyText)
This is the solution you should use to read and write.
with open('TestFile', 'r') as readfile:
copy = readfile.read()
print("Copy of textfile:\t", copy)
with open("copyTest.txt", 'w') as writefile:
length = writefile.write(copy)
print("Length written to file", length)
with open("copyTest.txt", 'r') as readfile:
copyText = readfile.read()
print("New file:\t", copyText)
output
Copy of textfile: this is a sentence
Length written to file 19
New file: this is a sentence
TestFile:
this is a sentence
It looks like the write function is outputting the number of characters that were written to the file, which means you are trying to call read on an int.
You'll want to store the file in a variable before trying to write to it, if you want to read in the file text afterwards. This can be achieved like the following
copy = open('TestFile').read() #Open 'TestFile', read it into variable
print("Copy of textfile:\t", copy)
copyFile = open('copyText.txt', 'w') #Create new file
copyFile.write(copy) # write in the copied text
copyText = copyFile.read()
print("New file :\t", copyText)

How to make a program that replaces newlines in python file with a string [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Closed 3 years ago.
I am trying to display my python file in html and therefore I would like to replace every time the file jumps to a newline with < br> but the program I've written is not working.
I've looked on here and tried changing the code around a bit I have gotten different results but not the ones I need.
with open(path, "r+") as file:
contents = file.read()
contents.replace("\n", "<br>")
print(contents)
file.close()
I want to have the file display < br> every time I have a new line but instead the code dosen't change anything to the file.
Here is an example program that works:
path = "example"
contents = ""
with open(path, "r") as file:
contents = file.read()
new_contents = contents.replace("\n", "<br>")
with open(path, "w") as file:
file.write(new_contents)
Your program doesn't work because the replace method does not modify the original string; it returns a new string.
Also, you need to write the new string to the file; python won't do it automatically.
Hope this helps :)
P.S. a with statement automatically closes the file stream.
Your code reads from the file, saves the contents to a variable and replaces the newlines. But the result is not saved anywhere. And to write the result into a file you must open the file for writing.
with open(path, "r+") as file:
contents = file.read()
contents = contents.replace("\n", "<br>")
with open(path, "w+") as file:
contents = file.write(contents)
there are some issues in this code snippet.
contents.replace("\n", "<br>") will return a new object which replaced \n with <br>, so you can use html_contents = contents.replace("\n", "<br>") and print(html_contents)
when you use with the file descriptor will close after leave the indented block.
Try this:
import re
with open(path, "r") as f:
contents = f.read()
contents = re.sub("\n", "<br>", contents)
print(contents)
Borrowed from this post:
import tempfile
def modify_file(filename):
#Create temporary file read/write
t = tempfile.NamedTemporaryFile(mode="r+")
#Open input file read-only
i = open(filename, 'r')
#Copy input file to temporary file, modifying as we go
for line in i:
t.write(line.rstrip()+"\n")
i.close() #Close input file
t.seek(0) #Rewind temporary file to beginning
o = open(filename, "w") #Reopen input file writable
#Overwriting original file with temporary file contents
for line in t:
o.write(line)
t.close() #Close temporary file, will cause it to be deleted

Print an array, dict, list into a text file on Python

I'm trying to print a list or dict of file names into a text file. it's currently only returning the first item on the list. the items are fetched from s3 Aws.I'm using Python 2.6
for obj in bucket.objects.filter(Prefix=prefix):
s = obj.key
with open('test.txt', 'w') as f:
f.write(s)
The problem here is that for every item, you create a new file (in case the file already exists, you remove the content so to speak), and then write s to it.
So you should swap the order of things here:
with open('test.txt', 'w') as f: # first open the file
for obj in bucket.objects.filter(Prefix=prefix): # then iterate
f.write(obj.key)
So we keep the file handle open, and each item will be written. A potential problem is that you will not write a new line after you written the key of an object. We can do this by writing a new line as well:
with open('test.txt', 'w') as f:
for obj in bucket.objects.filter(Prefix=prefix):
f.write(obj.key)
f.write('\n')
whenever you open a file for writing, the previous content is erased and new text is written. So in this case you are erasing whatever you wrote to the file in the next iteration. you can do it in this way or open the file in "append" mode and continue with what you have written.
f= open("test.txt", "w")
for obj in bucket.objects.filter(Prefix=prefix):
s = obj.key
f.write(s)
f.write('\n)
f.close()

Categories