I am having a moment of complete brain freeze right now and cannot seem to figure out how to design a code that will perform a series of tasks in one function. Basically, I want to design a code that asks the User for the input of a name of a folder in the working directory. The folders will have a 6 digit number for their name (for this example, let's assume the number of the folder is 111234). Once the folder is specified, certain files inside said folder will be opened and read. These files will be selected based on how their filenames are formatted, which is
(foldername)_(filenumber)_0_structure.in
wherein, for this example, the foldername is 111234 and the filenumber represents the order that the file appears in within the folder (can be the number zero or higher). The other terms in the filename (such as the zero after the filenumber), the word "structure", and the .in file extension are all constant. After all files that conform to this format are selected and opened, I want the 2nd and 3rd lines of these files to be read and copied into a dict whose keys are the file's filenumber and whose values contain a list of strings (i.e. the 2nd and 3rd lines).
So far, I have written the following in order to address these needs:
import os
from os import path
import re
def folder_validation(foldername):
folder_contents= {}
while True:
try:
foldername= str(raw_input(foldername))
file_path= path.join(current_directory, foldername)
except IOError:
print("Please give the name of a folder that exists in the current working directory.")
continue
for filename in os.listdir(file_path):
if re.search("{}_{}_0_detect1.in".format(foldername,[0*]), filename):
file_contents= str(open(filename).readlines()[2:3])
folder_contents[filenumber]= file_contents
return folder_contents
folder_input= folder_validation("Please give the name of the relevant folder you wish to analyze:")
The most obvious problem with the above code is that I am not sure how to format the regular expression search to include the user's input and the placement of any integer number in the filenumber variable. Additionally, the raw_input does not seem to be working. Any assistance would be most appreciated.