使用输入函数和while循环在Python中传输路径
当我定义使用输入函数和while循环在Python中传输路径,python,input,while-loop,path,Python,Input,While Loop,Path,当我定义文件路径和os.walk时,它运行良好: filePath = 'C:/Users/User/Desktop/test/' for subdir, dirs, files in os.walk(filePath): print(subdir) 输出: C:/Users/User/Desktop/test/ C:/Users/User/Desktop/test/a C:/Users/User/Desktop/test/a\a1 C:/Users/User/Desktop/tes
文件路径
和os.walk
时,它运行良好:
filePath = 'C:/Users/User/Desktop/test/'
for subdir, dirs, files in os.walk(filePath):
print(subdir)
输出:
C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/a
C:/Users/User/Desktop/test/a\a1
C:/Users/User/Desktop/test/a\a2
C:/Users/User/Desktop/test/a\a3
Please write your path:C:/Users/User/Desktop/test/
Please write your path:'C:/Users/User/Desktop/test/'
Please write your path:
Please write your path:C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/a
C:/Users/User/Desktop/test/a\a1
C:/Users/User/Desktop/test/a\a2
C:/Users/User/Desktop/test/a\a3
但是当我使用input
函数和循环时,我无法将filePath
传输到os.walk(filePath)
,我不知道为什么。有人能帮我弄清楚吗?谢谢
status = True
while status:
filePath = input(r"Please write your path:")
if filePath.strip() == "":
status = False
for subdir, dirs, files in os.walk(filePath):
print(subdir)
输出:
C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/a
C:/Users/User/Desktop/test/a\a1
C:/Users/User/Desktop/test/a\a2
C:/Users/User/Desktop/test/a\a3
Please write your path:C:/Users/User/Desktop/test/
Please write your path:'C:/Users/User/Desktop/test/'
Please write your path:
Please write your path:C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/a
C:/Users/User/Desktop/test/a\a1
C:/Users/User/Desktop/test/a\a2
C:/Users/User/Desktop/test/a\a3
编辑:
status = True
while status:
filePath = input(r"Please write your path:")
if os.path.exists(filePath):
status = False
for subdir, dirs, files in os.walk(filePath):
print(subdir)
输出:
C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/a
C:/Users/User/Desktop/test/a\a1
C:/Users/User/Desktop/test/a\a2
C:/Users/User/Desktop/test/a\a3
Please write your path:C:/Users/User/Desktop/test/
Please write your path:'C:/Users/User/Desktop/test/'
Please write your path:
Please write your path:C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/
C:/Users/User/Desktop/test/a
C:/Users/User/Desktop/test/a\a1
C:/Users/User/Desktop/test/a\a2
C:/Users/User/Desktop/test/a\a3
然后在这里结束,我想当代码重新运行时,我可以写另一个路径,比如:C:/Users/User/Desktop/test1/
,直到我写的路径为空,它将停止 使用os.path.exists()
来检查给定路径是否有效,而不是filePath.strip()==“”
试试:-
import os
status = True
while status:
filePath = input(r"Please write your path:")
if os.path.exists(filePath):
status = False
for subdir, dirs, files in os.walk(filePath):
print(subdir)
import os
status = True
while status:
filePath = input(r"Please write your path:")
if os.path.exists(filePath):
for subdir, dirs, files in os.walk(filePath):
print(subdir)
if filePath is "":
status = False
p.S.:-您对os.walk()
的命名方案不正确,os.walk()
,通常以Dirname、SubDir\u list、File\u list格式返回,因此您应该以正确的方式命名解包时使用的变量。您已将存储目录路径的变量命名为subdir
,将存储子目录列表的变量命名为dirs
,这可能会让其他人感到困惑。所以,试着按照惯例给它们命名,这样别人就更容易理解了
编辑:-
import os
status = True
while status:
filePath = input(r"Please write your path:")
if os.path.exists(filePath):
status = False
for subdir, dirs, files in os.walk(filePath):
print(subdir)
import os
status = True
while status:
filePath = input(r"Please write your path:")
if os.path.exists(filePath):
for subdir, dirs, files in os.walk(filePath):
print(subdir)
if filePath is "":
status = False
此代码将一直运行,直到将空字符串'
作为文件路径提供为止。(空字符串表示无任何意义)好的,谢谢您的帮助。还有一个问题:我使用的一些路径os.walk
既有斜杠又有反斜杠,示例:C:/Users/User/Desktop/test/a\a1
,C:/Users/User/Desktop/test/a\a3\a3\u1
,如何使用斜杠或反斜杠来获得路径,而不是混合使用?使用path.replace(“/”,“\”)
这将用反斜杠替换路径字符串中的任何正斜杠。例如,C:/Users/User/Desktop/test/a\a1
将变成C:\Users\User\Desktop\test\a\a1
。顺便说一句,不要忘记使用原始格式的路径字符串,以防止unicode解码错误。只需找到另一个问题,while循环不会迭代,当我键入一条路径时,我会得到os。walk
结果,它会停止。我希望迭代尽可能多的路径,除了空路径,循环将完成。对不起,我不能理解你想说什么,请你再解释一遍,用一些示例代码等。