0

Im trying to call a python file through a shell (CMD), but I get some issues.

When I run my .py file either by double clicking the .py file or through the Python IDLE editor, it runs just fine, and creates files just as it should.

But when I run it through command line, it runs through the file, but does not "open" / create a file.

I assume / think it might have something to do with the relative file path, but I'm not sure

The script is:

from __future__ import print_function
import sys
import os
import ctypes    
#-----File-Handling-------------------  

#Remove previous output file
if os.path.exists("Temp_py_out.txt"):
  os.remove("Temp_py_out.txt")

#create file:
tempfile = open("Temp_py_out.txt", "w+")
tempfile.write("0\nNo errors\nPar1\nPar2\nPar3\nPar4\nPar5\nPar6\n")

tempfile.close()
print("File OK")

EDIT: I can see it creates an file, but instead of in the folder where the .py is located, it creates it in my "C:\Users\meg" folder, which is the default folder my CMD is in when I open CMD.

How do I get python to force place it in the same folder as where the file is located?

2
  • How did you run the file using CMD? What was the invocation? Commented Jan 11, 2021 at 13:13
  • C:\Users\meg>python "C:\Users\meg\Desktop\PYTEST\test.py" and it responds with File OK just like my script ask it to do, so it is opening the script, it doest just not create the file in the folder my .py file is in EDIT: I can see that it does create a file!, but it creates it in my C:\Users\meg folder instead of where the file is located Commented Jan 11, 2021 at 13:14

2 Answers 2

1

Using this command to run

C:\Users\meg>python "C:\Users\meg\Desktop\PYTEST\test.py"

will use your current working directory C:\Users\meg, instead of the location of your Python file. Check that folder to see if there are files you're expecting.

Sign up to request clarification or add additional context in comments.

8 Comments

@Martin.E You need to change your working directory. When starting your Command Prompt, run cd C:\Users\meg\Desktop\PYTEST\ , then python test.py will do what you want.
Or you can use Python for that too: os.chdir('C:\\Users\\meg\\Desktop\\PYTEST\\'). Note that I used 2 backslashes, because in everywhere that's not Windows, backslashes are to denote special characters.
@Martin.E Use os.chdir(Path(__file__).parent.absolute()). To learn more, check it out here.
Thank you so much!, just what I was looking for, have an amazing day
Note chdir wants a string, so had to use os.chdir(str(Path(file).parent.absolute()))
|
1

u run the program from "c:\Users\meg" so probably the working directory for ur script will be on that path and not on "C:\Users\meg\Desktop\PYTEST\test.py"

you can test it out by writing the following line to ur code: print(os.getcwd())

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.