0

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.

Bob McBobson
  • 743
  • 1
  • 9
  • 29
  • What is the ```[0*]``` format argument supposed to represent? – wwii Feb 04 '17 at 03:51
  • ```foldername= str(raw_input(foldername))``` works fine in Python 2.7 - you need to add some details to ```raw_input does not seem to be working.``` – wwii Feb 04 '17 at 04:00
  • @wwii the [0*] was my attempt as trying to write an argument that would search for all files which had a number (zero or greater) in that part of their filename (i.e. after the second underscore). I know it is incorrect, but I do not know how to format such an argument. – Bob McBobson Feb 04 '17 at 12:31
  • 1
    There are a lot of online, python flavored, regex testers like [regex101](https://regex101.com/) - they are very useful for crafting regex patterns. You need to spend some time learning regular expressions. To match one or more digits/numbers you can use either ```[0-9]+``` or ```\d+``` - your format string would be - ```pattern = "{}_[0-9]+_0_detect1.in".format(foldername)``` – wwii Feb 04 '17 at 16:23
  • @wwii thanks for the heads up about regex101. I'll check it out further. But that being said, my raw_input still does not seem to work. I have probed it by typing in both correct and incorrect folder names, and the same result is always yielded– rather than returning my dict, it prints out the name of the given foldername, and then fails to complete the code. I have to use ctrl+C in order to interrupt the code. Further investigation seems that the code is stuck on the line `foldername= str(raw_input(foldername))` – Bob McBobson Feb 05 '17 at 12:36
  • 1
    Your while loop condition (```True```) never changes and you never do anything to break out of it. Maybe have a look at [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Feb 05 '17 at 17:00
  • Possible duplicate of [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Feb 05 '17 at 17:04

1 Answers1

0

There were two main problems in my code: the first problem was that I did not properly configure the while loop condition and so the code would get stuck. The second problem was that I did not set up the filepath to my files in my folder correctly, and as a result, my code could not open the files and read them. The regex line was also improved to include any filenames that had numbers 0 and above be read (in the specified format). The corrected version of the code is posted below.

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
       else:
            break 
    while True:    
        for filename in os.listdir(file_path):
            if re.search("{}_[0-9]+_0_detect1.in".format(foldername,[0*]), filename):
                file_contents= open(path.join(file_path,filename))
                file_lines= file_contents.readlines()[2:3]
                folder_contents[filename]= file_lines
    return folder_contents

folder_input= folder_validation("Please give the name of the relevant folder you wish to analyze:")
Bob McBobson
  • 743
  • 1
  • 9
  • 29