2

I'm doing a simple program that moves files, and I'm having it pull the file paths from a text document so it doesn't have to be inputed every time.

Here's the function for one of the buttons:

def XboxClick():
     pathfinder = open("settings.txt", "r")
     xboxsavepath = pathfinder.readline()
     steamsavepath = pathfinder.readline()
     pathfinder.close
     #lbl.configure(text=xboxsavepath)
     xboxsavefile = filedialog.askopenfilename(title="Select the Xbox save", initialdir=xboxsavepath)

In the text document, this row is C:/Users/username/AppData/Local/Packages/.

The commented out line #lbl.configure(text=xboxsavepath) will return C:/Users/username/AppData/Local/Packages/.

If I do:

xboxsavefile = filedialog.askopenfilename(title="Select the Xbox save", initialdir='C:/Users/username/AppData/Local/Packages/')

It will open the file window at that directory.

However, when using the variable xboxsavepath, which uses the exact same string, it just opens the file window to the default. I don't know why.

Hopefully that's enough info.

andrr
  • 23
  • 4

1 Answers1

2

The reason was quite hard to spot, but it is because there is a newline character at the end of your string because you are using readline.

From the docs:

f.readline() reads a single line from the file; a newline character (\n) is left at the end of the string....

If you want to see the newline(\n) then do something like print(repr(xboxsavepath)). So, strip the text that you get from the file so the newline character will be removed

xboxsavepath = pathfinder.readline().strip()

It is also better to strip all the lines you get from the txt, so even if there is an empty line at the end, you don't have to worry about it


You also need pathfinder.close() with the () to execute the method.

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
  • It got rid of \n, but it's still not using it as a directory. When doing `print(repr(xboxsavepath))`, it shows single quotes ('path/path') around xboxsavepath. Doing another `.strip("'")` doesn't appear to fix it. Or maybe that isn't the issue. – andrr Jan 03 '22 at 09:32
  • @andrr `repr` will show quotes around the string, it basically shows you how that string would look in your code, you can just copy-paste it and put it in python and it will work fine (unlike if you copy a string that is simply converted to string (just `print(...)`)) – Matiiss Jan 03 '22 at 11:52
  • @andrr The thing is, there _WILL_ be \n for each item, I tried this with an example and it fixed the issue for me, can you provide some more information? With \n I dont see a way possible for it to work – Delrius Euphoria Jan 03 '22 at 12:45
  • It looks like the problem was from the path having a file at the end of it. `D:/SteamLibrary/steamapps/common/GameExample/FSD/Saved/SaveGames/76561198207771097_Player.sav` does not work, but `D:/SteamLibrary/steamapps/common/GameExample/FSD/Saved/SaveGames` does. – andrr Jan 03 '22 at 15:58
  • @andrr Oh okay, but in your question you clearly said the label outputs the value just like you need it, and anyway you need `strip()` one way or another. – Delrius Euphoria Jan 03 '22 at 16:20
  • @andrr Hi, I hope you found this answer helpful, if so please mark it as correct answer to close this question :) – Delrius Euphoria Feb 16 '22 at 15:55