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?

Martin.E
  • 97
  • 8
  • How did you run the file using CMD? What was the invocation? – kwkt Jan 11 '21 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 – Martin.E Jan 11 '21 at 13:14

2 Answers2

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.

kwkt
  • 1,058
  • 3
  • 10
  • 19
  • You are right! I can see that - Is there a way to force Python to create the file in the same folder as where the .py File is located? – Martin.E Jan 11 '21 at 13:22
  • 1
    @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. – kwkt Jan 11 '21 at 13:23
  • 1
    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. – kwkt Jan 11 '21 at 13:26
  • But that will not work if I relocate the file to a new folder - I would like a relative path to the file it self inside python - Especially since its not only gonna be ran at my PC, but also others which does not have their users named meg – Martin.E Jan 11 '21 at 13:30
  • 1
    @Martin.E Use `os.chdir(Path(__file__).parent.absolute())`. To learn more, check it out [here](https://stackoverflow.com/a/54892023/2327379). – kwkt Jan 11 '21 at 13:32
  • 1
    Thank you so much!, just what I was looking for, have an amazing day – Martin.E Jan 11 '21 at 13:34
  • 1
    Note chdir wants a string, so had to use os.chdir(str(Path(__file__).parent.absolute())) – Martin.E Jan 11 '21 at 13:50
  • @Martin.E I just kinda copied verbatim, sorry I should have tested first. – kwkt Jan 11 '21 at 13:55
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())