Jump to content

Save files in python

Go to solution Solved by mikat,
39 minutes ago, Pythons said:

Hmmm, maybe.
I was thinking about just using normal python without downloading anything!

you don't have to, you can just do this:

 

read:

 

import json
with open('JSONData.json', 'r') as f:
     data = json.load(f)
 
print(data)

 

write:

import json
 
data = {
    "name":"Pythons",
    "type":"Linus Tech Tips User"
}
 
#json.dumps() method turns a Python data structure into JSON:
jsonData = json.dumps(data)
print(jsonData)
 
# Writing JSON data into a file called JSONData.json
#Use the method called json.dump()
#It's just dump() and not dumps()
#Encode JSON data
with open('JSONData.json', 'w') as f:
     json.dump(jsonData, f)

 

Hey everyone,

I have just created a save feature in my game. It uses

 

sfile = open("savefile","w")

 

to open the file, and

 

with open("savefile","w") as y:
            y.write("Attack " + repr(attack) + '\n')

to write in it.
I will store variables in this file like attack and defense. I just need a function that can read it and then change the variables depending on what was in the file.
Any help would be appreciated! ^_^

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/
Share on other sites

Link to post
Share on other sites

19 minutes ago, Pythons said:

Hey everyone,

I have just created a save feature in my game. It uses

 

sfile = open("savefile","w")

 

to open the file, and

 

with open("savefile","w") as y:
            y.write("Attack " + repr(attack) + '\n')

to write in it.
I will store variables in this file like attack and defense. I just need a function that can read it and then change the variables depending on what was in the file.
Any help would be appreciated! ^_^

If you're saving variables I'd recommend storing your stuff in json, here's a tutorial:

http://gowrishankarnath.com/read-write-json-python/

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/#findComment-9490304
Share on other sites

Link to post
Share on other sites

54 minutes ago, mikat said:

If you're saving variables I'd recommend storing your stuff in json, here's a tutorial:

http://gowrishankarnath.com/read-write-json-python/

Hmmm, maybe.
I was thinking about just using normal python without downloading anything!

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/#findComment-9490385
Share on other sites

Link to post
Share on other sites

39 minutes ago, Pythons said:

Hmmm, maybe.
I was thinking about just using normal python without downloading anything!

you don't have to, you can just do this:

 

read:

 

import json
with open('JSONData.json', 'r') as f:
     data = json.load(f)
 
print(data)

 

write:

import json
 
data = {
    "name":"Pythons",
    "type":"Linus Tech Tips User"
}
 
#json.dumps() method turns a Python data structure into JSON:
jsonData = json.dumps(data)
print(jsonData)
 
# Writing JSON data into a file called JSONData.json
#Use the method called json.dump()
#It's just dump() and not dumps()
#Encode JSON data
with open('JSONData.json', 'w') as f:
     json.dump(jsonData, f)

 

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/#findComment-9490449
Share on other sites

Link to post
Share on other sites

12 minutes ago, mikat said:

you don't have to, you can just do this:

 

read:

 


import json
with open('JSONData.json', 'r') as f:
     data = json.load(f)
 
print(data)

 

write:


import json
 
data = {
    "name":"Pythons",
    "type":"Linus Tech Tips User"
}
 
#json.dumps() method turns a Python data structure into JSON:
jsonData = json.dumps(data)
print(jsonData)
 
# Writing JSON data into a file called JSONData.json
#Use the method called json.dump()
#It's just dump() and not dumps()
#Encode JSON data
with open('JSONData.json', 'w') as f:
     json.dump(jsonData, f)

 

Hey, Thanks a heap!!!

Just one last question: How do I assign these values to a variable?

So, for this example,

 

Name = "Pythons"

 

Sorry if this is a silly question, I'm not very good at python as you can see xD

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/#findComment-9490468
Share on other sites

Link to post
Share on other sites

15 minutes ago, Pythons said:

Hey, Thanks a heap!!!

Just one last question: How do I assign these values to a variable?

So, for this example,

 

Name = "Pythons"

 

Sorry if this is a silly question, I'm not very good at python as you can see xD

data['name']

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/#findComment-9490495
Share on other sites

Link to post
Share on other sites

13 minutes ago, Pythons said:

Hey, Thanks a heap!!!

Just one last question: How do I assign these values to a variable?

So, for this example,

 

Name = "Pythons"

 

Sorry if this is a silly question, I'm not very good at python as you can see xD

username = data["name"]

Link to comment
https://linustechtips.com/topic/749658-save-files-in-python/#findComment-9490496
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×