【发布时间】:2019-06-25 13:15:22
【问题描述】:
我正在 Lynda 上做一些 Python 学习教程。正如人们所期望的那样,本教程的脚本在与脚本相同的目录中创建文件。
不过,无论出于何种原因,我的安装正在创建文件并希望在项目根目录或两个目录中读取文件(使用路径对象)。
我正在运行的脚本是C:\Users\user\Development\Ex_Files_Learning_Python\Exercise Files\Ch4
脚本如下所示:
import os
from os import path
import datetime
from datetime import date, time, timedelta
import time
def main():
# Print the name of the OS
print(os.name)
# Check for item existence and type
print("Item exists: " + str(path.exists("textfile.txt")))
print("Item is a file: " + str(path.isfile("textfile.txt")))
print("Item is a directory: " + str(path.isdir("textfile.txt")))
# Work with file paths
print("Item path" + str(path.realpath("textfile.txt")))
print("Item path and name: " + str(path.split(path.realpath("textfile.txt"))))
# Get the modification time
# Calculate how long ago the item was modified
if __name__ == "__main__":
main()
它的输出是
nt
Item exists: False
Item is a file: False
Item is a directory: False
Item pathC:\Users\user\Development\Ex_Files_Learning_Python\textfile.txt
Item path and name: ('C:\\Users\\user\\Development\\Ex_Files_Learning_Python', 'textfile.txt')
如您所见,它假定它的路径是项目根目录,向上两个目录。我在之前的练习中遇到了同样的问题,创建了一个文件。当我在文件对象上使用 open() 时,它在项目的根目录中创建了文件。
感谢任何指针。
更新:我已经确定这是因为我使用的是 VSCode 终端。如何指示 VSCode 终端从我正在编辑和调试的文件的 cwd 而不是项目根目录运行程序?
为了记录,这是我的调试器的launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Remote Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": ".",
"remoteRoot": "."
}
]
},
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "enter-your-module-name-here",
"console": "integratedTerminal"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
【问题讨论】:
-
如果你在
C:\users\user\Development\Ex_Files_Learning_Python并做python "Exercise Files\Ch4.py",你会得到这种行为。如果您愿意,可以重新根到 Python 文件:os.chdir(__file__) -
谢谢,查看我的更新@AdamSmith 这是默认为根目录的 VSCode 终端。现在想弄清楚如何解决这个问题。另一方面,我可能是错的,因为我没有尝试过,但你的重新root 解决方法不会因为它尝试将目录更改为文件路径而不是目录而失败吗?
-
哦,是的,它完全可以。
os.chdir(os.path.dirname(__file__))然后:)。有一种方法可以更改vscode中的这些设置,但这取决于您加载文件的方式(它是项目的一部分等) -
@AdamSmith 我将它作为项目的一部分打开。我打开了目录,然后从侧边栏打开项目文件。
标签: python python-3.x visual-studio-code