如何返回到Python 3的if语句?

2024-09-25 00:28:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作一个Python程序,在那里我可以处理来自计算机任何部分的文件。还没做完,但我遇到了一个问题。这是我的密码:

import os
from os.path import join
import subprocess
def opening_file(lookfor):
    global store_1
    for root, dirs, files in os.walk('/home/'):
        if lookfor in files:
           file = join(root, lookfor)
           store_1.append(join(root, lookfor))
    if len(store_1) <= 0:
        ask_1 = str(input("Your file was not found. Do you want to try again(Y/n)?:"))
        #This is where I have the problem
    elif len(store_1) > 0:
        print("Your file was found")
        subprocess.call(["xdg-open", file])
    #print(store_1)
store_1 = []
print("Welcome to our program for working with files.")
print("Press O for opening and editing a file, C for making a copy of a file. M for moving a file and R for renaming a file. If you are done working with the file, press F to end the program.")
choice = str(input("Your choice:"))
if choice == "O" or choice == "o":
   lookfor = input("File name(make sure to include the extension as well):")
   opening_file(lookfor)

我想知道在找不到文件时,如何返回用户输入的if语句。你知道吗

有什么办法我能做到吗?我在谷歌上搜索过,但找不到解决问题的办法。我的操作系统是ubuntu16.04。你知道吗


Tags: thetostoreimportforifosroot
2条回答

只需使用while?你知道吗

import os
from os.path import join
import subprocess
def opening_file(lookfor):
    global store_1
    for root, dirs, files in os.walk('/home/'):
        if lookfor in files:
           file = join(root, lookfor)
           store_1.append(join(root, lookfor))
    if len(store_1) <= 0:
        ask_1 = str(input("Your file was not found. Do you want to try again(Y/n)?:"))
        #This is where I have the problem
    elif len(store_1) > 0:
        print("Your file was found")
        subprocess.call(["xdg-open", file])
    #print(store_1)
store_1 = []
print("Welcome to our program for working with files.")
choice = ""
while(choice.lower() != "f"):
    print("Press O for opening and editing a file, C for making a copy of a file. M for moving a file and R for renaming a file. If you are done working with the file, press F to end the program.")
    choice = str(input("Your choice:"))
    if choice == "O" or choice == "o":
       lookfor = input("File name(make sure to include the extension as well):")
       opening_file(lookfor)
import os
from os.path import join
import subprocess
def opening_file(lookfor):
    store_1 = []
    for root, dirs, files in os.walk('/home/'):
        if lookfor in files:
           file = join(root, lookfor)
           store_1.append(join(root, lookfor))
    if len(store_1) <= 0:
        ask_1 = str(input("Your file was not found. Do you want to try again(Y/n)?:"))
        #This is where I have the problem
        if (ask_1.lower() == 'y':
            return 'y'
        else:
            return 'n'
    else:
        print("Your file was found")
        subprocess.call(["xdg-open", file])
        return store_1
        #print(store_1)

def show_menu():
    choice = str(input("Your choice:"))
    if choice == "O" or choice == "o":
        lookfor = input("File name(make sure to include the extension as well):")
        result = opening_file(lookfor)
        if result == 'y':
            show_menu()
        elif result == 'n':
            #your code for no
        else:
            # you can manage the result list 'store_1'
            print(result)

print("Welcome to our program for working with files.")
print("Press O for opening and editing a file, C for making a copy of a file. 
M for moving a file and R for renaming a file. If you are done working with the file, press F to end the program.")
show_menu()

相关问题 更多 >