[英]Forced to enter full pathname every time instead of just file name
Every time I try to do an action such as running a program in the VScode Terminal (also happens in PyCharm) I have to enter in the full pathname instead of just the file name.每次我尝试执行诸如在 VScode 终端中运行程序(也发生在 PyCharm 中)之类的操作时,我都必须输入完整的路径名,而不仅仅是文件名。 For example, instead of just doing
python3 test.py
to run a program, I have to enter in python3 /Users/syedrishad/Desktop/Code/test.py
.例如,我必须输入
python3 /Users/syedrishad/Desktop/Code/test.py
,而不是仅仅执行python3 test.py
来运行程序。
Now, this is annoying and all but it doesn't bother me too much.现在,这很烦人,但它并没有太困扰我。 What does bother me is when my program is trying to pull/ open files from somewhere else.
困扰我的是当我的程序试图从其他地方拉/打开文件时。 If I wanted an image call Apple.jpeg, instead of just typing in Apple.jpeg, I'd have to go and find the full pathname for it.
如果我想要一个名为 Apple.jpeg 的图像,而不仅仅是输入 Apple.jpeg,我必须 go 并找到它的完整路径名。 If I were to upload a piece of code doing this to someplace like GitHub, the person who'd want to test this code out for themselves will have to go in and replace each pathname with just the file name or it won't work on their computer.
如果我将执行此操作的一段代码上传到 GitHub 之类的地方,想要自己测试此代码的人将不得不 go 并用文件名替换每个路径名,否则它将无法正常工作他们的电脑。 This problem has been going on for a while, and I sadly haven't found a solution to this.
这个问题已经持续了一段时间,遗憾的是我还没有找到解决方案。 I would appreciate any help I get.
我会很感激我得到的任何帮助。 I'm also on a Mac if that makes a difference.
如果这有所作为,我也在 Mac 上。
You could use os and sys that gives you the full path to the folder of the python file.您可以使用 os 和 sys 为您提供 python 文件的文件夹的完整路径。
Sys gives you the path and os gives you the possibility to merge it with the file name. Sys 为您提供路径, os 为您提供将其与文件名合并的可能性。
import sys, os
print(sys.path[0]) # that is the path to the directory of the python file
print(sys.path[0]+'/name.txt') #full path to the file
print(os.path.join(sys.path[0],'name.txt')) # os.path.join takes two parameters and merges them as one path using / but the line above is also fine
In VS Code, its internal terminal is in the currently opened project folder by default.在 VS Code 中,它的内部终端默认在当前打开的项目文件夹中。 Therefore, when you use the command "python file_name.py" to run the file, the terminal cannot find the file that exists in the inner folder.
因此,当您使用命令“python file_name.py”运行该文件时,终端找不到内部文件夹中存在的文件。 Therefore, in addition to using the file path, we can also add related settings to help it find the file.
因此,除了使用文件路径外,我们还可以添加相关设置来帮助它找到文件。
Run: When using the run button to execute the file, we can add the following settings in "settings.json", it will automatically enter the parent folder of the executed file.运行:当使用运行按钮执行文件时,我们可以在“settings.json”中添加如下设置,它会自动进入执行文件的父文件夹。
"python.terminal.executeInFileDir": true,
"When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder." “在终端执行文件时,是否使用在文件目录中执行,而不是在当前打开的文件夹中执行。”
debug: For debugging code, we need to add the following settings in "launch.json": debug:为了调试代码,我们需要在“launch.json”中添加如下设置:
"cwd": "${fileDirname}",
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.