To "read a file in", if it's a text file (human-readable lines of around 80 characters), you normally do it line by line. That looks like this:
with open(filename) as file:
for line in file:
# whatever you wanted to do with each line
But in your case, you only want to read the very first line, and don't care about anything else, right? So:
with open(filename) as file:
first_line = next(file)
# whatever you wanted to do with first_line
Now, how do you deal with that line? A lot of people would suggest a regular expression, and \s(\d+)f\d+w(\d+)\.
should do it, but you're not likely to understand that.
One way to do it is to find places to "split" the line to get the parts you want.
# Input menu file: 10f160w39.feedme
If you separate that by spaces, everything you care about is in the last word, 10f160w39.feedme
. (There may also be invisible whitespace on the end—in fact, there is at least a newline character. I'll use strip
to throw them away first.) So, let's start with:
menufile = line.strip().rsplit()[-1]
Now, everything before the f
is one number, and everything between the w
and the .
is another right? So:
f = menufile.partition('f')[0]
w = menufile.rpartition('.')[0].rpartition('w')[-1]
And we're done.