Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between enumerate(fileinput.input(file)) and enumerate(file)

I'm looking for some help with my code which is rigth below :

for file in file_name :
    if os.path.isfile(file):
        for line_number, line in enumerate(fileinput.input(file, inplace=1)):
            print file
            os.system("pause")
            if line_number ==1:
                line = line.replace('Object','#Object')
                sys.stdout.write(line)

I wanted to modify some previous extracted files in order to plot them with matplotlib. To do so, I remove some lines, comment some others.

My problem is the following :

  • Using for line_number, line in enumerate(fileinput.input(file, inplace=1)): gives me only 4 out of 5 previous extracted files (when looking file_name list contains 5 references !)

  • Using for line_number, line in enumerate(file): gives me the 5 previous extracted file, BUT I don't know how to make modifications using the same file without creating another one...

Did you have an idea on this issue? Is this a normal issue?

like image 812
flor14n Avatar asked Jun 23 '15 14:06

flor14n


People also ask

What is Fileinput in Python?

input() in Python. With the help of fileinput. input() method, we can get the file as input and to can be used to update and append the data in the file by using fileinput.

How do you take an external input in Python?

You just write simple program and then run it from command line in any platform like Windows/ Linux. <,> are redirection operators which simply redirects the stdin and stdout to input. txt and output. txt.


1 Answers

There a number of things that might help you.

Firstly file_name appears to be a list of file names. It might be better named file_names and then you could use file_name for each one. You have verified that this does hold 5 entries.

The enumerate() function is used to help when enumerating a list of items to provide both an index and the item for each loop. This saves you having to use a separate counter variable, e.g.

for index, item in enumerate(["item1", "item2", "item3"]):
    print index, item

would print:

0  item1
1  item2
2  item3

This is not really required, as you have chosen to use the fileinput library. This is designed to take a list of files and iterate over all of the lines in all of the files in one single loop. As such you need to tweak your approach a bit, assuming your list of files is called file_names then you write something as follows:

# Keep only files in the file list
file_names = [file_name for file_name in file_names if os.path.isfile(file_name)]

# Iterate all lines in all files
for line in fileinput.input(file_names, inplace=1):
    if fileinput.filelineno() == 1:
        line = line.replace('Object','#Object')
        sys.stdout.write(line)  

The main point here being that it is better to pre filter any non-filenames before passing the list to fileinput. I will leave it up to you to fix the output.

fileinput provides a number of functions to help you figure out which file or line number is currently being processed.

like image 178
Martin Evans Avatar answered Sep 30 '22 15:09

Martin Evans