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

后端 未结 3 858
一整个雨季
一整个雨季 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

    It seems you want to check if the directory exists.

    If so, see os.path.isdir.

    os.path.isdir(path)
        Return True if path is an existing directory.
        This follows symbolic links, so both islink()
        and isdir() can be true for the same path.
    

    You can do like this:

    s = raw_input();
    if os.path.isdir(s):
        f = open(s, "r+")
    else:
        print "Directory not exists."
    

提交回复
热议问题