python - 我怎样才能回到 Python 3 中的 if 语句?
问题描述
我正在制作一个 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 语句。
有什么办法可以做到这一点吗?我用谷歌搜索,但我找不到解决我的问题的方法。我的操作系统是 Ubuntu 16.04。
解决方案
只需使用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)
推荐阅读
- cakephp - 在(新)实体上关联/分离相关实体
- dsl - 根据空属性过滤 Apache Atlas 实体
- java - 是否可以将用户输入从文本字段转换为代码?
- java - 如何将 ENUM 值传递给从 androidx.appcompat.widget.AppCompatButton 扩展的类的构造函数的参数
- charts - ECharts - 更改条形图线高
- java - 在 Spring Boot >= 2.4.3 中匹配 RepositoryRestResource 端点的尾部斜杠
- react-native - 使用 axios 发出获取请求时出现 404
- python - 将 request.GET 的响应呈现为 iframe
- python - 如何使用 Flask 蓝图实现 Flask-Dance
- wordpress - 修改商店列表 - 电子主题