Hello. I load a text file with topics on each line like so: with…
- User
Hello. I load a text file with topics on each line like so: with open("occult_topic_list_3k.txt", 'r') as f: occult_topics = [line.strip() for line in f]
each topic has a file (if it's been completed) in the folder
tasks/. I want to find the line number to start at to only include lines in occult_topics that dont have corresponding filenames.i.e. if one line is economics, there would be a tasks/economics.txt file if it'd be completed already
- Assistant
Sure, let's write a Python script to find the first topic in the list that doesn't yet have a corresponding file in the
tasks/folder. We'll also return the line number:pythonimport os with open("occult_topic_list_3k.txt", 'r') as f: occult_topics = [line.strip() for line in f] # Path where the task files are stored path_to_tasks = "tasks/" start_line = None for i, topic in enumerate(occult_topics): # Form the expected filename filename = path_to_tasks + topic + ".txt" if not os.path.exists(filename): # If the file doesn't exist, we've found our start line start_line = i break # Adding 1 to the line number since line numbers typically start from 1 in text editors, not 0 print(f"Start processing from line number: {start_line + 1 if start_line is not None else 'All topics completed'}") - User
Now edit this to make it so I can start building the list from a specific line number: with open("occult_topic_list_3k.txt", 'r') as f: occult_topics = [line.strip() for line in f]
- Assistant
Of course, to start reading from a specific line number, you can modify the script like this:
pythonimport os # Define the start line here (0-based index) start_line = 10 # Change this to the line number you want to start from - 1 with open("occult_topic_list_3k.txt", 'r') as f: # Skip the first `start_line` lines for _ in range(start_line): next(f) # Then read the rest occult_topics = [line.strip() for line in f] # Path where the task files are stored path_to_tasks = "tasks/" first_uncompleted_line = None for i, topic in enumerate(occult_topics, start=start_line): # Form the expected filename filename = path_to_tasks + topic + ".txt" if not os.path.exists(filename): # If the file doesn't exist, we've found our start line first_uncompleted_line = i break # Adding 1 to the line number since line numbers typically start from 1 in text editors, not 0 print(f"Start processing from line number: {first_uncompleted_line + 1 if first_uncompleted_line is not None else 'All topics completed'}")