0

The username of my computer is ghbjnk. Wherever I saved my Pyscript and ran it, I always get C:\Users\ghbjnk rather than the current working directory, why? I'm using VScode and Python3.0 The weird thing is, if I debug the script Here is my Pyscript:

def main():
    import os
    print(os.path.abspath('.'))

if __name__ == "__main__":
    main()
Kaiyan
  • 21
  • 1
  • os.getcwd() also doesn't work – Kaiyan Apr 10 '20 at 00:22
  • 3
    Are you running the program in vscode? How do you know what the CWD shoiuld be? Does it work from the command line? Basically, `os.getcwd()` is right - I mean definitively right. You just aren't where you think you are. – tdelaney Apr 10 '20 at 00:30
  • It's because you are running this program using the extension so you are getting the present working directory. Type `pwd` from you will get the path. – saibhaskar Apr 10 '20 at 00:36
  • I voted to close as a dup - assuming this is an issue with configuring the vscode debugger. I have no idea why this was downvoted, though. – tdelaney Apr 10 '20 at 00:54
  • The current working directory you execute the script from (shown in VS Powershell terminal prompt you debug it, e.g. `PS C:\Users\Andrew`) can be different to the directory your Python script is saved. If you want to get the directory containing the Python script, you could try: `os.path.dirname(os.path.realpath(__file__))` https://stackoverflow.com/questions/5137497/find-current-directory-and-files-directory – anjsimmo Apr 10 '20 at 01:22

0 Answers0