简体   繁体   中英

Running python file through VS Code terminal

My question is I am unable to run a python file in VS Code terminal unless I specify the full path. Whereas, when I see any python tutorial, only python filename is entered and it works. Can somebody pls help me with this issue?

The files need to be in the same folder where you are operating / or have the other Python files.

You can use two ways for this:

first:

import os
pwd = os.getcwd()

# again, make sure you file is in the same directory. 

the_file = (pwd + "\\filename.xlsx") 

or Secondly,

#the below you can use wherever your file is and it will locate it. 
# you specify the full path using pathlib.path and:
pathlib.path(directory here without the file itself).joinpath(file name here).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM