Search code examples
pythonloopspython-itertools

How to loop itertools through a list of files?


I manage to create this for single file to slice down the line in a file. I would like to take odd number only.

filename = sys.argv[1]
filename2 = sys.argv[2]

with open(filename) as f:
    items = itertools.islice(f, 0, None, 2)

    x = (list(items))
    final = "".join(x)

with open(filename2, 'a+') as bb:
    bb.write(final)

Above code is working fine. But I need to specify the file name manually. What if I had 100 files that I need to slice?

Example what above code output. It will slice/remove the line on even number.

Input file
a
b
c
d
e

Output file
a
c
e

I'm thinking of loop. So far here's my solution.

# Directory where it contains my pat files.
mydir = "C:\\Users\\Desktop\\Scripts\\"
files = filter(lambda x: x.endswith('.pat'), os.listdir(mydir))

# print(list(files))
# ['filepat1.pat', 'filepat2.pat', 'filepat3.pat'.......]

i = 0
new_file = 'file{}.pat'.format(i)

for file in files:
    with open(file) as f:
       items = itertools.islice(f, 0, None, 2)
       x = (list(items))
       final = "".join(x)
       #print(final)
       for item in items:
           i +=1
           new_file = 'file{}.pat'.format(i)
           with open(new_file, 'a+') as ww:
               ww.write(final)

What is my mistake? Seems like it missing the loop.


Solution

  • Correct me if I'm wrong, but the code from for item in items: looks strange. From what I understand, you are trying to pull odd lines from one file and store them in a new file, right?

    You can rewrite like this:

    mydir = "C:\\Users\\Desktop\\Scripts\\"
    files = filter(lambda x: x.endswith('.pat'), os.listdir(mydir))
    
    #print(list(files))
    
    for i, file in enumerate(files):
        with open(file) as f:
           items = itertools.islice(f, 0, None, 2)
           x = (list(items))
           final = "".join(x)
           #print(final)
    
           new_file = 'file{}.pat'.format(i)
           with open(new_file, 'a+') as ww:
               ww.write(final)