Order of files parsed into my jupyter notebook
0
0
Entering edit mode
7 days ago
Bianca • 0

Hello, I am very very new to python.

I have defined a function to parse some files using pysam and added them to a list in my Jupyter notebook using something like:

def grab_all_files_(fdir):
files_ = []
for f in glob.glob(fdir + '/*.bam'):
bams.append(pysam.AlignmentFile(f, "rb"))
return files_


How do I know the order in which the files are listed? (I am using python 3.9.12) For example, how do I know which file is files_[0], files_[1]...

Thank you

python Jupyter python3.9.12 • 224 views
0
Entering edit mode

Looking past that after the line def grab_all_files_(fdir):, there should be indentation...
As written, I don't see you adding anything into the list files_?

0
Entering edit mode

You are right. I fixed my question, sorry. It was correct in my notebook, though. How do I know the order in which the files are listed? (I am using python 3.9.12) For example, how do I know which file is files_[0], files_[1]... please?

1
Entering edit mode

You fixed the indenting of the function some. (The return line should be indented, too, I suspect.) However, you aren't adding anything to files_ still with current code you show here (pasted below with posted error(s) intact).

def grab_all_files_(fdir):
files_ = []
for f in glob.glob(fdir + '/*.bam'):
bams.append(pysam.AlignmentFile(f, "rb"))
return files_


Is anything getting returned from grab_all_files_ when you actually run your code?

As for how do you know the order ... They'd be in the order that glob.glob() iterates on them which I assume is how the files are listed in your system? (You can easily test this in a separate notebook, by running glob.glob(fdir + '/*.bam') in a cell.) Usually you don't need to know the order because either you make a file that has a name similar to the input or you use a dictionary to track the correspondences.

0
Entering edit mode

Thank you so much! That answered my question. Oh, the function works as is... (: