1

In atom, os.getcwd() always returns D:\WorkSpace\Test. So if I do things like open("01.txt"), it cannot find the file.

Also, this happens when using the "script" package to execute in Atom, But when executing the actual python file, it works.

I have found several other asking the same question, like this, but there is still no resolution.

Thanks to everyone who tried to help!

Added my directory:

D:\WorkSpace\Test
D:\WorkSpace\Test\01\01.py
D:\WorkSpace\Test\01\01.txt
 or  
D:\WorkSpace\Test
                └─01
                    └─ 01.py
                    └─ 01.txt

Added my source:
01.py

import os
print os.getcwd()
f = open("01.txt")
print f.read()

01.txt

atom editor 01.txt

Added results(in atom):

D:\WorkSpace\Test
Traceback (most recent call last):
  File "D:\WorkSpace\Test\01\01.py", line 5, in <module>
    f = open("01.txt")
IOError: [Errno 2] No such file or directory: '01.txt'

Added results(in windows cmd):

D:\WorkSpace\Test\01>01.py
D:\WorkSpace\Test\01
atom editor 01.txt
Community
  • 1
  • 1
Menng
  • 11
  • 1
  • 4
  • 1
    See this question: “[Reading file using relative path in python](http://stackoverflow.com/questions/40416072/reading-file-using-relative-path-in-python-project/40416154#40416154)” for a reliable way to read data files that live alongside your python code. No matter what the current directory is. – spectras Nov 16 '16 at 08:21
  • @spectras Thanks for your comments, working as it should. – Menng Nov 16 '16 at 08:37
  • This isn't about getting a current working directory in Atom, but rather about doing pathing in python. – Matthew Schuchard Nov 16 '16 at 12:28

2 Answers2

1

In Mac, I solved this kind of issue by opening ATOM from a shell window, under target directory. Seems ATOM will use the directory it inherited from shell process as its working directory. You might try with a windows cmd window, see if it works.

moling
  • 11
  • 1
1

Windows - go to Packages -> Settings View -> Manage Packages. Then go to System Settings on the left hand side menu and tick 'Show in file context menus'.

You can now go to your chosen directory and open any file (.js, .py etc.) with Atom and the current working directory will change to the one you chose instead of the default .atom.

foxroot
  • 11
  • 1