繁体   English   中英

如何在python中创建返回到先前条件的循环

[英]How to create a loop in python that returns to a previous condition

我正在尝试编写一个条件不存在时循环返回的代码。

我正在研究程序的开始,对python来说还很新。 我希望我的代码检查文件是否有效,是否继续进行下一步,是否不请求文件的新路径。 关于最佳方法的任何想法吗? 我还想检查文件类型是否正确,但是还没有找到与我所需类似的代码。 我想,但是最主要的是让它循环。

现在我要结束了,它返回了是否检索文件。 我可以再次复制并粘贴exist语句,但是我知道必须有更好的方法来做到这一点。 任何帮助,将不胜感激。

# Imports OS module to allow interaction with underlying operating system
import os
# Imports time functions
import time

# Greet the user
print ("Welcome to Project Coded Phish.")
# Ask user for the path to the text file they would like to use
print ("Please provide a valid path to the chat log text (.txt) file.")
#save the path from the user input
path1 = input ()
exists = os.path.isfile(path1)
# if the file can be found
if exists:
    print ("File was successfully retrieved.")
# if it isn't found
else:
    print ("Please provide a valid path to the chat log text (.txt) file.")
    path1 = input ()

如果找到路径,它将打印正确的单词。 它只是打印“请提供聊天记录文本(.txt)文件的有效路径”。

尝试这个:

path1 = input ()
while not os.path.isfile(path1):
    print ("Please provide a valid path to the chat log text (.txt) file.")
    path1 = input ()
print ("File was successfully retrieved.")

这可以通过while循环轻松完成:

while True:
    exists = os.path.isfile(path1)
    # if the file can be found
    if exists:
        print ("File was successfully retrieved.")
        # since condition is met, we exit the loop and go on with the rest of the program
        break
    # if it isn't found
    else:
        print ("Please provide a valid path to the chat log text (.txt) file.")
        path1 = input ()

你可以试试

import os

def ask_for_filepath():
    input_path = input("Please provide a valid path to the chat log text (.txt) file.")
    return input_path

input_path = ask_for_filepath()

while os.path.isfile(input_path) is False:
    input_path = ask_for_filepath()

尝试这个:

while True:
    path1 = input()
    exists = os.path.isfile(path1)
    if exists and path1.endswith('.txt'):
        print("File was successfully retrieved.")
        with open(path1) as file:
            # do something with file
            break
    else:
        print("Please provide a valid path to the chat log text (.txt) file.")

While循环将继续循环直到break语句。 以“ with”开头的部分代码称为上下文管理器,用于打开文件。 endswith方法将检查文件的扩展名。

您可以使用如下递归函数来实现此目的:

# Imports OS module to allow interaction with underlying operating system
import os
# Imports time functions
import time

# Greet the user
print ("Welcome to Project Coded Phish.")
# Ask user for the path to the text file they would like to use
print ("Please provide a valid path to the chat log text (.txt) file.")
# if the file can be found
def check():
    path1 = input ()
    exists = os.path.isfile(path1)
    if exists:
        print ("File was successfully retrieved.")
        return
    # if it isn't found
    else:
        print("file not found")
        check()

check()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM