Mike Honeycutt
Mike Honeycutt

Reputation: 3

Python won't create / write to a file

I am new at python and learning the language. The following code should create a file in the running program directory and write to it but it doesn't do this at all in a .py file. If I put the same code in the IDLE shell it returns 17. No errors just doesn't create the file. What am I doing wrong?

with open("st.txt", "w") as f:
    f.write("Hi from Python!")

Thanks for the help Mike

Upvotes: 0

Views: 1237

Answers (2)

Carson
Carson

Reputation: 152

your code works well, st.txt will be touched at executing path.

other ways, your system account can't write in your execute path.

try in your $HOME path to execute your code, I think, It will work well

Upvotes: 0

ashburshui
ashburshui

Reputation: 1410

This code is flawless, no problem!

I guess that in your REPL shell, the $PWD environment variable is set for somewhere, so your destination file is in some corner.

No exception thrown indicates that no problem with access authority.

Maybe you can set some absolute path string, such as ~/st.txt

By the way, the successful invoke should return 15 instead of 17, totally count 15 chars.

Upvotes: 2

Related Questions