【发布时间】:2021-07-18 05:30:45
【问题描述】:
我正在编写一个脚本来获取我当前的工作目录,将其连接到“cd”并将该字符串写入文件。但是,每当我尝试指定路径时,都会出现以下错误:
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)
任何帮助将不胜感激。我想写这个命令,这样当我深入文件树时,我可以保存它的位置,以防我需要打开另一个窗口。谢谢!
【问题讨论】:
-
可能以
r'/home/cameron..'开头 -
使用
os.getcwd()查找当前工作目录。评论你得到了什么 -
谢谢你们! Sid 你的解决方案奏效了。我相信“r”是一个错字,对吧?