0
votes

So basically user gives a path to the file in txt file. I can print out the path without any problems but when I try to open the file I get the error "No such file or directory"

  • And it returns the path that the user gave but with a doubled slash like that c://.
  • I know that in order to order open file path needs to be with double slash, but program cant find such path... I don't think code is needed here but I will put it below just in case. ( there is also some other stuff there, program just changes certain letters to another letters)
count = len(open(
    "C:\\plik.txt", "r+").readlines())
dd = []
with open("C:\\plik.txt", "r+") as f:
    i = []
    for _ in range(count):
        d = f.readline()
        print(d)
        i.append(d)
dlugosc = len(i)
for _ in range(dlugosc):
    dd.append(list(i[_].split("!")))
print(dd)
fraza_do_zamiany = dd[0][1]
fraza_nowa = dd[1][1]
path = dd[2][1]
print(path)

with open(path, "r+") as f:
    a = []
    aaa = []
    for _ in f:
        a.append(str(_))
    for i in range(len(a)):
        b = a[i]
        b = list(b)
        for _ in range(len(b)):
            if b[_] == fraza_do_zamiany:
                b[_] = fraza_nowa
            else:

                pass
        aa = ""
        for _ in range(len(b)):
            aa = aa + b[_]
        b = []
        print(aa)
        aaa.append(aa)
    f.truncate(0)
    f.seek(0)
    for _ in range(len(aaa)):
        f.write(aaa[_])

Your file path appears to be directly in your C: drive and not in your projects directory--where the .py file is stored. Try moving the .txt file to the same folder as the .py and then call the file as just "plik.txt". - SlavaCat
Well, the problem isn't with this path but with the path that the user gave in txt file. I gave full path because in vsc I cant open files just by their name even if they are in same folder. - Michał Wisełka
What's the file given in the txt file? - SlavaCat
here is a path that is in txt file "C:\Users\Michał\Desktop\tow.txt" and it leads to just another txt file - Michał Wisełka
what does print(path) output in line 17? - SlavaCat