Shadylamp
Shadylamp

Reputation: 83

file.read() doesn't work in Python

So I'm new to programming and I'm making a zork-type game as extracurricular practice. And currently I'm trying to make a simple "load" function where python would extract the previously saved name and class of character. The code is this:

print "Enter savefile name: " 
save1 = raw_input("> ") + ".py"
load_data = open(save1, "r")
data = load_data.read()

However it doesn't read the file. So if I'd write something like print data it would just skip the command, without any errors or anything really.

I've been looking at this stupid piece of code for ages and can't figure out what's the problem with it. Please help!

Thank you

Upvotes: 0

Views: 2060

Answers (2)

Phayer
Phayer

Reputation: 45

This will create a text file with a custom name, if that's what you mean

sname=str(input("Enter savefile name: "))
fw=open(sname+'.txt', 'w')
fw.close()

Upvotes: 0

usumoio
usumoio

Reputation: 3568

Have you checked at each stage that something is actually being returned. Try something like so:

if save1:
    print "save1 is correct"

if load_data:
    print "load_data is correct"

if data:
    print "data has a value"

Try these lines after your code block. This will help you identify where the error is, and once your problem is isolated you will be able to better search for what went wrong.

Upvotes: 2

Related Questions