0

I'm working on a program for school that asks the user what they would like to name a file and then I'm supposed to write to that file.

So far I have this:

dream_file = input("What file name would you like to save the cards? ")
dream_file = open(dream_file, 'w')

dream_file.write(str(dream_hand1))
print(dream_file)

dream_file.close()

When I run it I get this error: <_io.TextIOWrapper name='dream' mode='w' encoding='US-ASCII'>

And as far as I know the file never gets created.

6
  • What is dream_hand1? Are you sure this is your entire relevant code? Commented Jul 12, 2018 at 18:21
  • What is dream_hand1? Commented Jul 12, 2018 at 18:21
  • It's a variable that I have to write to the file. Commented Jul 12, 2018 at 18:22
  • the rest of the code is in a function. seems like it's too long for me to post the whole thing. Commented Jul 12, 2018 at 18:24
  • <_io.TextIOWrapper name='dream' mode='w' encoding='US-ASCII'> is not an error; it's the output of print(dream_file) Commented Jul 12, 2018 at 18:27

3 Answers 3

1

A file is definitely being written, but as others have mentioned you are simply printing out the string representation of the file handle's python representation. If you want to print the file contents, you only need to make a couple changes.

# it is poor practice to reuse variable names
# for completely different things. It is best
# to differentiate your file path and the file
# handler itself.
dream_file_path = input("What file name would you like to save the cards? ")

# w+ allows reading and writing of files
dream_file = open(dream_file_path, 'w+')

dream_file.write(str(dream_hand1))

# seek 0 brings you back from where you just
# wrote (end of the file), to the beginning
dream_file.seek(0)

# .read() simply reads the entire file as a string
print(dream_file.read())

dream_file.close()
Sign up to request clarification or add additional context in comments.

3 Comments

also we haven't learned about "seek" so i'm not able to use that.
'w' opens a file for writing, and 'w+' opens for reading and writing.
No problem. Although you say you haven't learned it yet, your professor is not likely to think you were cheating or something for using the .seek() method on a file in python. Seek is only one of a handful of commonly used methods for a file handle including read, readlines, write, writelines, and seek, and all of these are described well in the python io documentation here: docs.python.org/3/tutorial/inputoutput.html. If anything not already taught was expressly forbidden then alright, but the use of .seek() is unlikely to cause any suspicion of cheating.
0

That's not an error. An error would come with a clear error message and it would have a stack trace, along with lines of code and whatnot. I think that what you have here is what comes out when you do

print(dream_file)

That statement doesn't print the file contents. In fact, it can't, because you're opening the file in write mode. Instead, it prints the string representation of the dream_file, which is an object of type _io.TextIOWrapper. If you want to print the string you just put into the file, you can instead do

print(str(dream_hand1))

Try looking for the new file in the folder your code is located in, or exploring the input and output functionality of python to get a better understanding of how it works.

Comments

0

your file was created by using the 'w' in the open function , and " <_io.TextIOWrapper name='dream' mode='w' encoding='US-ASCII'> " comes from print(dream_file) wich means dream_file is a _io.textIOWrapper Object.

check the directory in that your python is, you should find a file named as you input and with dream_hand1 data inside.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.