-2

I am making a Python program where I can work with files from any part of my computer. It's not done yet but I ran into a problem. This is my code:

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)

I want to know how can i go back to the if statement the user entered with his/her input when the file is not being found.

Is there any way I can do this? I have googled but I cannot find a solution to my problem. My OS is Ubuntu 16.04.

Sayse
  • 42,633
  • 14
  • 77
  • 146
Mahir Islam
  • 1,941
  • 2
  • 12
  • 33

2 Answers2

3

Simply use 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)
Ochmar
  • 294
  • 2
  • 11
1
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()
wailinux
  • 139
  • 4