I am new at programming and I have written a script to extract text from a vcf file. I am using a Linux virtual machine and running Ubuntu. I have run this script through the command line by changing my directory to the file with the vcf file in and then entering python script.py
.
My script knows which file to process because the beginning of my script is:
my_file = open("inputfile1.vcf", "r+")
outputfile = open("outputfile.txt", "w")
The script puts the information I need into a list and then I write it to outputfile. However, I have many input files (all .vcf
) and want to write them to different output files with a similar name to the input (such as input_processed.txt
).
Do I need to run a shell script to iterate over the files in the folder? If so how would I change the python script to accommodate this? I.e writing the list to an outputfile?
You don't need write shell script, maybe this question will help you?
How to list all files of a directory?
I would integrate it within the Python script, which will allow you to easily run it on other platforms too and doesn't add much code anyway.
To output the resulting files in a separate directory I would:
You can use listdir(you need to write condition to filter the particular extension) or glob. I generally prefer glob. For example
This code will read the content from input and store it in outputfile.
It depends on how you implement the iteration logic.
If you want to implement it in python, just do it;
If you want to implement it in a shell script, just change your python script to accept parameters, and then use shell script to call the python script with your suitable parameters.
I have a script I frequently use which includes using PyQt5 to pop up a window that prompts the user to select a file... then it walks the directory to find all of the files in the directory:
You need to import os to use the os.listdir command.