I'm a beginner at python programming and now I want to try to write a function consisting of my already written programs. The goal is to get all probabilities when I'm calling on the function in the main program.
My already separately written programs looks something likes this and the goal of these programs is to extract information from different files until I reach the last file to be able to extract some values.
#program 1 f1=open('filename1', 'r') newfile=('file_name1', 'w') for lines in f.readlines(): if 'number' in lines: do some stuff and save it to newfile.write() f.close() newfile.close()
#program 2 f2=open('filename1', 'r') # open file created in newfile in program 1 newfile=('file_name2, 'w') for lines in f2.readlines(): f3=open('filename3', 'r') for data in f3.readlines(): if lines.split('\t') in datat: write to new_file f3.close() f2.close() new_file.close()
# program 3 #here I'm extracting all values from a certain column from a file using 'filename2' f5=open('file_name2', 'r') # open file created in newfile in program 2 newfile=('value_file','w') for lines in f5.readlines(): f6= open('filename6', 'r') for data in 'filename6': if lines[column2] exist in data: then write all these values to a new_file f6.close() f5.close() new_file.close()
How do I create a function out of this to use in the main program. So I can just get all values automatically? I don't know where to start other than with def. Can one combine several programs into one fcn or do I need to create several functions? The important thing is program 3 where I'm getting all the values i'm interested in. But to get there I need to go through all these other files and search. Because then i'm gonna use these values in already written formula.
Edited 5 Years Ago by Ezzaral: Added code tags. Please use them to format any code that you post.