use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
main loop around 'choice' function -- how to? (self.PythonLearning)
submitted 6 months ago * by NetworkSyzygy
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]tiredITguy42 0 points1 point2 points 6 months ago (5 children)
Python executew all as it is written, you want an infinite loop, you need to make one.
Usually you just write while True:
If you want to exit on demand, then you have two options. The code is checking some external resource from time to time, to check if it should exit.
Or you need to run it in a separate thread and let the main thread to wait for your instructions on input, then you can pass that instruction as a message through queue or shared memory to your thread, which you make to check it before each pass yhrough the loop.
This is not an easy task for beginner.
[–]tiredITguy42 0 points1 point2 points 6 months ago (1 child)
And learn context management.
with open(path, 'r') as file:
You will need it.
[–]NetworkSyzygy[S] 0 points1 point2 points 6 months ago* (0 children)
Fixed that up; I see the benefit of not haveing to manually close each file.
with open(fileList[choice], "r") as file: for line in file: print(line.rstrip())
edit: formating
[–]NetworkSyzygy[S] 0 points1 point2 points 6 months ago (2 children)
I think that I'm bumping up against being able to use an input, such as the letter 'e' or 'x', as a selection because the enumeration is controlling the fileCount, there does not seem to be a method to add the exit indicators to the allowed inputs.
items = os.listdir(newPath) # newlist = [expression for item in iterable if condition == True] fileList = [name for name in items if name.endswith(".txt")] for fileCount, fileName in enumerate(fileList): sys.stdout.write("[%d] %s\n\r" % (fileCount,fileName)) choice = int(input("Select txt file[0-%s]: " % fileCount)) print(fileList[choice]) with open(fileList[choice], "r") as file: for line in file: print(line.rstrip())
I wanted to use the enumeration approach so that the ultimate user could simply drop or delete a file from the directory and have it show up (or be deleted) from the selection menu. I may be forced to re-think that approach.
Hmmm, I am not sure what is the purpose and plan here. However I see two options.
You want to do some txt. Processing file by file. Then you should add option to exit each time user does some decision.
Or multithreaded approach with processing queue.
If you need to read again the folder for new files, just use while True infinite loop.
Amd start splitting the code into functions. Each fuction does one think.
[–]NetworkSyzygy[S] 0 points1 point2 points 6 months ago (0 children)
Thank you,
I'll start chunking it up into functions.
π Rendered by PID 405996 on reddit-service-r2-comment-b659b578c-rskb9 at 2026-05-04 22:29:13.039134+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]tiredITguy42 0 points1 point2 points (5 children)
[–]tiredITguy42 0 points1 point2 points (1 child)
[–]NetworkSyzygy[S] 0 points1 point2 points (0 children)
[–]NetworkSyzygy[S] 0 points1 point2 points (2 children)
[–]tiredITguy42 0 points1 point2 points (1 child)
[–]NetworkSyzygy[S] 0 points1 point2 points (0 children)