0

I am working on a script to get my present workind directory, concatenate it to "cd" and write that string to a file. However whenever I try to specify the path I get the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'home/cameron/projects/personal/new_window/last_directory.txt'
import os
import sys
from pathlib import Path

# Get working directory and transform into command string
stream = os.popen('pwd')
output = "cd " + stream.readline()

path = 'home/cameron/projects/personal/new_window/last_directory.txt'

# Open and write command to file
file = open(path,'w')
file.write(output)
file.close()

# Print status
print("Current Directory written:")
print(stream)

Any help would be much appreciated. I'd like to write this command so that when I'm deep in a file tree I can save it's location in the case I need to open another window. Thanks!

1 Answers1

0

path likely needs to start with a / for your OS.

The directories in the path must also exist prior to writing. You can make them if they don't yet exist with:

if not os.path.exists(path):
     os.makedirs(path)
JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19