简体   繁体   English

每次都强制输入完整路径名,而不仅仅是文件名

[英]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.

相关问题 Python - 打印文件名显示的完整路径,而不仅仅是文件名 - Python - Printing file name show's full path instead of just the file name 通过GUID而不是路径名识别文件 - Identifying a file by GUID instead of by pathname 如何递归地打印每个文件或文件夹的路径名 - how to print the pathname of every file or folder recursively 如何测试文件是否包含完整路径或文件名,Python? - How to Test if file contains full path or just file name, Python? Matplotlib 总是为每个 plot 打开一个 Window 而不是仅仅将其写入文件 - Matplotlib always opens a Window for every plot instead of just writing it to a file 将实际文件添加到列表中,而不仅仅是文件的字符串名称 - Adding actual files to a list, instead of just the file's string name 蟒蛇。 每次按ENTER键,将文件中的文本显示为20行片段 - Python. Showing text from a file in fragments of 20 lines every time ENTER is pressed 如何配置 axios 以要求特别<domain_name> /<path> 只需编写路径,而不必每次都对域名进行硬编码</path></domain_name> - How to configure axios to request to particluar <domain_name>/<path> just by writing path without having to hardcode domain name every time 递归返回文件的路径名 - Recursively return the pathname of the file 从文件的每一行删除回车 - Removing enter from every line of file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM