简体   繁体   English

通过 VS Code 终端运行 python 文件

[英]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.我的问题是我无法在 VS Code 终端中运行 python 文件,除非我指定完整路径。 Whereas, when I see any python tutorial, only python filename is entered and it works.然而,当我看到任何 python 教程时,只输入了 python 文件名并且它可以工作。 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.这些文件需要位于您正在操作/或具有其他 Python 文件的同一文件夹中。

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).

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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