What is the proper way to take a directory path as user input?

后端 未结 3 859
一整个雨季
一整个雨季 2020-12-31 08:34

Below is a snippet of code I am trying to use to take a directory path as \"raw input\" from the user. I receive the following error after the input is taken from the user:<

3条回答
  •  感动是毒
    2020-12-31 09:11

    I think you should try something like:

    import sys
    import os
    
    user_input = raw_input("Enter the path of your file: ")
    
    assert os.path.exists(user_input), "I did not find the file at, "+str(user_input)
    f = open(user_input,'r+')
    print("Hooray we found your file!")
    #stuff you do with the file goes here
    f.close()
    

提交回复
热议问题