Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Python Selecting an inputted number of files from a folder

ftoner

New Coder
I have a folder with a few hundred thousands data files in it. And what I want to do is separate the data into groups of n (which will be inputted by the user) in a way that allows me to manipulate just that group of data. Then I want to start back where I left off in the folder and take another group. For example if n was 5 I would want files 1-5 to be read and manipulated then I'd like to have 6-10 read and so on and so forth.

I have tried using a for loop and the yield keyword but I was having a problem with a generator error. I would like to continue using the for loop with just a different technique of grabbing the data.
 
Difficult to advise without having details.
You should post the code that you wrote for this, and describe exactly what the error was and where it occurred.
 
Python:
def fileavg(path,n):
    import numpy as np
    import xlsxwriter
    from glob import glob
    from itertools import islice

    workbook = xlsxwriter.Workbook('Test.xlsx')
    worksheet = workbook.add_worksheet()
    row=0
    more_files=True
    b=glob.iglob(path) #when inputting path name begin with r' and end with a '
    next_n_files=list(islice(b,n))
   
    while more_files:
        for i in range(n):
            try:
                next_file=next(next_n_files)
                print(row,next_file)
                A=np.mean(next_file)
            except StopIteration:
                more_files=False
                break
           
            for col, data in enumerate(A):
                worksheet.write_column(row, col, data)
            row +=1
[CODE]
The error I'm receiving is "Out[26]: <generator object fileavg at 0x000002ACE4B70270>"
 
Last edited by a moderator:
def fileavg(path,n):
import numpy as np
import xlsxwriter
from glob import glob
from itertools import islice

workbook = xlsxwriter.Workbook('Test.xlsx')
worksheet = workbook.add_worksheet()
row=0
more_files=True
b=glob.iglob(path) #when inputting path name begin with r' and end with a '
next_n_files=list(islice(b,n))

while more_files:
for i in range(n):
try:
next_file=next(next_n_files)
print(row,next_file)
A=np.mean(next_file)
except StopIteration:
more_files=False
break

for col, data in enumerate(A):
worksheet.write_column(row, col, data)
row +=1

The error I'm receiving is "Out[26]: <generator object fileavg at 0x000002ACE4B70270>"
Now I'm receiving a name error indicating glob is not defined even after messing with the glob import line
 
Please always post your code in proper code tags using the </> button. Absolutely necessary for Python as the code makes no sense without the indentation. And if you get an error, post the full context including the stack trace and such.

As for glob not being defined, I could be wrong but I thought import statements should be at the very top of the code.
 

Buy us a coffee!

Back
Top Bottom