sorting out the common path in a python script with different functions
0
0
Entering edit mode
6.8 years ago
bio90029 ▴ 10

Hi, I need a little bit of help. I have a script that contains four different functions. Three of the function have a common pathway but they deal with different type of data, example below:

def One():
    #do something
One()

def Two(path_to_folder_H*,something_else):
    #do something with files

folders=glob.glob(path_to_folder_H*)
for folder in folders:
    my_file=glob.glob(file.fastqs)
    Two(path_to_folder_H*,something_else)

def Third(path_to_folder_H*, someting_else_2):
    #do something

folders=glob.glob(path_to_folder_H*)
for folder in folders:
    my_file=glob.glob(file.fa)
    Third(path_to_folder_H*,something_else_2)

def Fouth(path_to_folder_H*,something_else_3):
    #do someething

folders=glob.glob(path_to_folder_H*)
for folder in folders:
    my_file=glob.glob(file.xml)
    Fourth(path_to_folder_H*,something_else_3)

I would like to tidy up the 'for loops' but I don't know how to manage that. Can anyone help me in this matter, please? Thanks

python • 1.4k views
ADD COMMENT
1
Entering edit mode

maybe I am missing the point of the question. first I do not know If this a bioinformatic question. But why you do not write only one function with three argument one of them is a condition based on it you decide what to do?

ADD REPLY
0
Entering edit mode

But the for loops do nothing... They just. I can't. Hrm. And what's up with my_file? I believe OP needs to rethink the code.

ADD REPLY
0
Entering edit mode

that is why I commented

I am missing the point

ADD REPLY
1
Entering edit mode

Hello bio90029!

We believe that this post does not fit the main topic of this site.

This is a programming question. Please either show us how this is bioinformatics or search/ask StackOverflow.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

ADD REPLY
0
Entering edit mode

I think that is pretty mean.

ADD REPLY
1
Entering edit mode

No it's pretty standard, we don't specialize in pure programming here.

ADD REPLY
0
Entering edit mode

It is more about coherence of the form to bioinformatics, and he also gave you a good place for programming issues stack-overflow. Otherwise you could explain how this is much related to bioinformtics and people here will be more than happy to help :)

ADD REPLY
1
Entering edit mode

As others have said this is a pure programming question, not bioinformatics so doesn't really belong here. It's also not very clear what you're trying to do?

If each function is supposed to find a different file/filetype I'd use something like:

import os, fnmatch
for root, dirnames, filenames in os.walk(path_to_folder):
    for filename in sorted(fnmatch.filter(filenames, '*.ext')):
          #do something with matched extension

But you could find the various files outside of each function, and just pass the necessary file to each function, rather than finding it all over again every time.

ADD REPLY

Login before adding your answer.

Traffic: 2839 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6