Unable to write to txt files in Python



I’ve just started using Atom on Mac for my computing projects written in Python. I installed the “atom-python-run” package to easily run my programs in the terminal.

It mostly works fine however I don’t seem to be able to save or read from txt files. When using the code below through Atom it either doesn’t write to the file or prints blank lines when reading. I tried the same code in IDLE and it works perfectly fine so I believe it a problem with Atoms permissions not being able to access files? Any idea where I could fix this?
The other problem related to this is that if I try to run a Python file that’s in a folder and not on the desktop, I get an error saying the file doesn’t exist. It seems like Atom doesn’t have the correct permissions but as I’m knew to Mac I’m unsure how to fix it. Any ideas?

string = "Lots of words"
f = open("textfile.txt", "w")




It’s not a permission issue unless you’re trying to write or read to a directory that’s protected somehow. You are also mistaken about your assumption that Atom is writing to or reading anything. Atom has no ability to understand Python, and nor does atom-python-run. What atom-python-run does is tell Atom how to send a Python script to the python command you have installed on your computer.

Try this script via atom-python-run and tell me what it says.

import os

print os.path.realpath(__file__)
print os.getcwd()

Is that the folder you’re trying to create a new file in?


Thanks for the reply, this is the error I get when trying to run the code you sent me.

  File "/Users/logan/Desktop/test.py", line 3
    print os.path.realpath(__file__)
SyntaxError: invalid syntax

Why can I only run files from the desktop though?
This is the error I get when trying to run a file through Atom in a folder other than the desktop.
If I run files through IDLE they work perfectly fine. Any ideas why the writing to txt file doesn’t work?

(null): can't open file '/Users/logan/Desktop/School': [Errno 2] No such file or directory

Thanks for any further help.


Okay. That means you’re using Python 3 and you should use this code:

import os


I was under the impression that Mac came with Python 2 by default. Is that not the case, or do you have both installed?

Why can I only run files from the desktop though?

Are you sure it’s only from the desktop? How many other places have you tried? Please be specific and thorough; I need to be able to reconstruct what you have done in my head in order to give you an answer.

Any ideas why the writing to txt file doesn’t work?

We’ll find out when we learn what the script outputs.


Is this the name of a directory? Are there any special characters or spaces after School?


Yeh Macs do come with Python 2 by default but I installed 3 because thats the version we use at school etc.
This is what I get when I run the code:


Does this explain the txt file?

As for running the code from different folders, I’ve just realised it didn’t work from the folder it was in as its called “School Work”. Thanks for noticing that, I’d completely overlooked the space in the name.

Thanks for the help so far.


I seem to be getting another problem now.

I was running the equivalent of this code through a certain folder:

import random

names = ["Bob", "John", "Kevin"]

I get this error:

Traceback (most recent call last):

File "/Users/logan/Desktop/Computing/Homework/ranom.py", line 4, in <module>


AttributeError: module 'random' has no attribute 'choice'

But if I run it anywhere else it works perfectly fine. It also seems to have generated a folder in that folder called “pycache”.

Any ideas what is happening here? I’m having so many problems with Atom that its probably not worth using.


You’re not having any problems with Atom. You’re having problems with the atom-python-run package, how the Pythons are configured on your machine, and library importing. Atom is working perfectly well through this whole thing and the issues seem to be with how the package is delivering the payload of your script to the interpreter. Before you look for a different code editor, I would look for a different code-running package. I’m a fan of process-palette.

AttributeError: module 'random' has no attribute 'choice'

This sounds like there might be another file named random.py that’s getting imported instead of the standard library version. Comment out the rest of ranom.py and try this:

import sys
import random



Yeh I see. It’s just if its running through the terminal like idle does, why can’t I write to txt files?

I deleted random.py and ran the code again and it worked perfectly fine, thanks for the suggestion. I completely forgot it’d look for another file and not the library version.

I think thats most things sorted now, just me making mistakes aha. Any idea about the txt files though?




Did you consider using an absolute path?

Depending on the package you use… The package creates a secondary temporary file to execute your code. The location of this temporary file is not what you expect it to be. Absolute path may help if my guess is correct of what the issue is.



That’s what I’m trying to figure out. It works just fine for me. I haven’t changed any settings of atom-python-run and this script works whether I’m using Python 2 or 3.


That’s odd, do you have any other packages installed? Where did you save the file to?

I’ll try again when I get back.


Just realised that you’re running on windows though? I think it must be some permission issue on Mac as it works perfectly fine through idle but not on atom with the runner package. Could be something to do with the path like Dan suggested?


What happens if you run the script with the python command from the Terminal?

Yes, many, but nothing except for atom-python-run and Python itself is relevant.

Where did you save the file to?

You can see in the GIF that the file is saved to the current folder, which is the path that is retrieved with os.getcwd().

Have you tried making a file with an absolute path such as /Users/logan/Desktop/output.txt?


So I saved the following code in a folder on my desktop. /Users/logan/Desktop/pythonpractice/test.py

f = open("text.txt", "w")
f.write("Hello world")

And ran it using the python command from the terminal and it worked fine. It created the text file and wrote to it successfully.
I then deleted the text file and ran the code again through atom. It didn’t send back an error but the file wasn’t created in the same place. I had a look around and found it under Users/logan

Any ideas how I can change the path that it uses to the exact path of the file?



Hi Logan,

As you have IDLE, consider opening this IDE and trigger the Help -> Python Docs menu option. Search the for the word directory. See which options are available to force the system to use a specified work directory / path.

The alternative is to specify the full path when specifying the text file to write to.



What folder is open in Atom?


What do you mean? Like where is the script file saved?


The problem isn’t with pythons directory. It the package that I am using. It doesn’t use the exact path as the saved file. So when a text file is created it is saved somewhere else. Any ideas how to change this?

I guess I’m probably best to specify the path of the file like you suggested.


No, the folder that is open in Atom as the project folder, like how I had the file-writing folder open in my screenshot.


A program can be initiated / triggered from any path in your computer. The path where the program is stored does not automatically become the work-from directory. The program can however change the work-from directory from trigger-path to whatever you choose.

Now please read my notes as those words from a layman (me). My specific knowledge of Python is paper-thin. @DamnedScholar is far more knowledgeable.

Good luck to you.