I use this script to recursively go through a directory and view file contents. However, when I type in the file's contents, it shows at the top and reprints the entire list of files again. I just want the list of file paths to print once and then the user can open as many files as they want without the list printing each time. Hope that makes sense.
import os
while True:
for root, dirs, files in os.walk("/home"):
for file in files:
print(os.path.join(root, file))
fname = raw_input('Enter filepath to view '
'(leave empty to proceed without viewing another file):')
if not fname:
break
f = open(fname, 'r')
print f.read()
Move printing the directory tree outside of while loop:
import os
for root, dirs, files in os.walk("/home"):
for file in files:
print(os.path.join(root, file))
while True:
fpath = raw_input('Enter filepath to view '
'(leave empty to proceed without viewing another file):')
if not fpath:
break
with open(fpath, 'r') as f:
print f.read()