在Python 3.9中使用目录路径接受用户输入时发生os.chdir(src_path)错误



代码段:

src_path = input("enter the src direc : " )
print("src path ",src_path)
dst_path = input("enter the dest direc: " )
os.chdir(src_path) # It errors at this line

在接受用户输入目录后出现此错误。

enter the src direc : "C:Src_html"  # (<< This is what i entered as the path, also tried giving "C\ ' same error
src path  "C:Src_html"
enter the dest direc: "C:Src_html"
dest path  "C:Src_html"

当代码到达这里时,它会抛出一个错误,如下所示

os.chdir(src_path)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '"C:\Src_html"'

问题是用户输入无效:

enter the src direc : "C:Src_html"

用户输入"strong";C: \ Src_html"而不是C:\Src_html。因此,src_path保存一个路径,路径的开始和结束处都有一个",并且作为str对象(因为输入返回str),它用另一个'再次包装该路径。所以src_path看起来是这样的:";C: \ Src_html"',双"

你可以在按摩上看到,这就是它所保持的路径:

OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '"C:\Src_html"'

最新更新