饺影 发表于 2022-7-3 12:25:32

python报错问题

def bianji(hanghshu):
    bianji_path = ".\\temp\\bianji.txt"
    print("已进入编辑模式")
    temp =
    for temp_2 in temp:
      temp_2 = (str(input(">>>")))
      with open(bianji_path,"a") as f:
            f.write(temp_2)

    print("请输入要保存的路径")
    Save_path = (str(input(">>>")))
    temp = open(Save_path,"w")
    temp_2 = open(bianji_path,"r")
    s = temp.read()
    w = temp_2.write(s)
    temp.close()
    temp_2.close()

报错是
Traceback (most recent call last):
File "I:\lomen cmd\main.py", line 15, in <module>
    data.storehouse.bianji(hangshu)
File "I:\lomen cmd\data\storehouse.py", line 21, in bianji
    with open(bianji_path,"w") as f:
FileNotFoundError: No such file or directory: '.\\temp\\bianji.txt'

这是vscode中两个文件的路径:
py文件:I:\lomen cmd\data\storehouse.py
txt文件:I:\lomen cmd\data\temp\bianji.txt

求大佬解惑,也有可能是我疏忽了,谢谢

临时号 发表于 2022-7-3 12:59:42

从你的报错来看,你的main.py应该在I:\lomen cmd\main.py这个路径下
你现在在main.py中调用了storehouse.py的方法,但是你的路径依然是在I:\lomen cmd\这个路径下
所以你打开的路径补全其实是I:\lomen cmd\temp\bianji.txt,当然找不到文件
所以应当改成bianji_path = ".\\data\\temp\\bianji.txt"
def bianji(hanghshu):
    bianji_path = ".\\data\\temp\\bianji.txt"
    print("已进入编辑模式")
    temp =
    for temp_2 in temp:
      temp_2 = (str(input(">>>")))
      with open(bianji_path,"a") as f:
            f.write(temp_2)

    print("请输入要保存的路径")
    Save_path = (str(input(">>>")))
    temp = open(Save_path,"w")
    temp_2 = open(bianji_path,"r")
    s = temp.read()
    w = temp_2.write(s)
    temp.close()
    temp_2.close()

jackz007 发表于 2022-7-3 13:52:58

本帖最后由 jackz007 于 2022-7-3 13:55 编辑

       代码文件(.py)所在目录 “I:\lomen cmd\data” 内有个名为 “temp” 的子目录?
页: [1]
查看完整版本: python报错问题