user2965964
user2965964

Reputation: 13

Use user input as path to save file in Python 3?

I'm writing a script to download a file from a website, and I'm able to successfully save the file using a path entered into the code, however if I use an input then things don't work.

path = input("Save Location: ")

From here I'll use os.path.join to append the file type to the end of the path and then use PycURL to download the file. But getting a user input for the path gives a FileNotFoundError, for example C:/Users/MyName/Desktop become C:UsersMyNameDesktop/v.mp4 after appending the file type. I've also tried C:\\Users\\MyName\\Desktop as well as C:\/Users\/MyName\/Desktop however they give the same thing, and ideally I'd like to avoid using double forward/back slashes in the input since they're not very user friendly.

If for whatever reason you need any more code/all the code don't hesitate to ask. Thanks :)

Upvotes: 1

Views: 1820

Answers (1)

Paweł Kordowski
Paweł Kordowski

Reputation: 2768

try to use https://docs.python.org/3/library/os.path.html#os.path.normpath

>>> x = input()
C:/Users/MyName/Desktop
>>> os.path.normpath(os.path.join(x, 'v.mp4'))
'C:\\Users\\MyName\\Desktop\\v.mp4'

Upvotes: 1

Related Questions