Program creation

I am using the following code to create a dynamic file editor from any point on the SSD the problem is the last line of code is never executed so the editor is never called although the program does correctly find the file to be edited. Any ideas?

print("----------------File Editor-------------------")
import subprocess
import os
print()
filename = input("Enter the File Name to Edit: ")
def find_files(filename, search_path):
   result = []
   for root, dir, files in os.walk(search_path):
      if filename in files:
         result.append(os.path.join(root, filename))
         print("--------working---------")
   return result
print(find_files(filename,"/"))
subprocess.call = (['sudo' , 'nano' , filename])

The last line is an assignment, where I guess you wanted to call the subprocess.call function.
Try removing the =.

2 Likes

Thanks I did remove the = sign and the editor now loads asking for the sudo password, but the intended file is not loaded into the editor it tries to create a blank file by that name

By Brett Bojanoski via Discussions on Python.org at 06Aug2022 01:57:

Thanks I did remove the = sign and the editor now loads asking for the
sudo password, but the intended file is not loaded into the editor it
tries to create a blank file by that name

Let’s review your code:

filename = input("Enter the File Name to Edit: ")
def find_files(filename, search_path):
   result = []
   for root, dir, files in os.walk(search_path):
      if filename in files:
         result.append(os.path.join(root, filename))
         print("--------working---------")
   return result
print(find_files(filename,"/"))
subprocess.call(['sudo' , 'nano' , filename])

You’ve invoking:

sudo nano filename

but filename is the original filename you read from input()
earlier. What you likely want is one of the files located by
find_files().

You run find_files but do not store or use its result.

Try something like this:

found_files = find_files(filename,"/")
print(found_files)
if found_files:
    chosen_filename = found_files[0]
    print("editing", chosen_filename)
    subprocess.call(['sudo' , 'nano' , chosen_filename])

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Thanks for the idea I entered your code after mine and now the program does nothing more that ask for the filename and say command not found and then exits. Is this where I was supposed to enter your code?

By Brett Bojanoski via Discussions on Python.org at 06Aug2022 15:07:

Thanks for the idea I entered your code after mine and now the program
does nothing more that ask for the filename and say command not found
and then exits. Is this where I was supposed to enter your code?

It was meant as a replacement of the bottom part of your code. But it
would help to see your current code and the output from running it.
Preferably as text pasted into a post between backticks:

```
pasted code/output here
```

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

thanks for the help the code works the 2nd time I tried it I am not sure why it did not work the first time.