I'm doing some learning python tutorials on Lynda. The tutorial has the script creating the files in the same directory as the script, as one would expect.
For whatever reason, though, my installation is creating the file and looking to read the file (using the path object) in the project root, or two directories up.
The script I am running is C:\Users\user\Development\Ex_Files_Learning_Python\Exercise Files\Ch4
The script looks like this:
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()
Its output is
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')
So as you can see, it assumes its path is the project root, two directories up. I had the same problem in my previous exercise, creating a file. When i used open() on a file object, it created the file two directories up in the root of the project.
Any pointers are appreciated.
Update: I've established that this is happening because I'm using the VSCode terminal. How do I instruct the VSCode terminal to run the program from the cwd of the file i'm editing and debugging, rather than the project root?
For the record, here's my launch.json for the debugger
{
// 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"
}
]
}