I am using python to take a file (protein sequence file) from user. I want to know how can I keep a check if a user specifies a specific file /typeformat.
for example, the function is :
def user_file():
file_var = input("Enter your file name: ")
with open(file_var, 'r') as obj:
print (obj.read())
As I am working with protein sequence files, so I want to check if user has entered a "input.phy"
(a sequences file in phylip format) then a block of statements are printed / calculated and if user has entered a "input.aln"
(A sequence file in clustalw format) , then another conditional block of statements are printed and if user has entered a "input.fa"
(a sequence file in FASTA format), then a 3rd block of statements are printed.
I am confused about how the specific file type will be checked?
please don't. every-time someone write such interactive program, god kills a kitten,
just parse the cmd-line arguments https://docs.python.org/2/howto/argparse.html
just check the extension of the file ?
but anyway, you should never trust a user: parse the input and throw an exception if the file is badly formatted.
Yes, you are right. I would prefer to check both the file type (extension) and the format of the file and also throw an exception if the file is badly formatted.
So is there a way I can check the file type using python? Can I get some help? I tried using this also,
but couldn't get successful.
I'm working on a certain program and I need to have it do different things if the file in question is a "phy" (phylip file), or a "aln" (clustal file) or "fa"(fasta file). Could I just use this?
Note: When I use that, it tells me invalid syntax. So what do I do?
Are you just concerned about the file extension or do you also want your program to check the contents?
I am preferably concerned with the extension of the file but I would check both the file type (extension) and the format of the file and also throw an exception if the file is badly formatted.
Hi mdsiddra,
Please give feedback on your previous thread: Text file to Phylip format
If an answer was helpful you should upvote it, if the answer resolved your question you should mark it as accepted.
Yes, I have given feedback there already.
Great, but you haven't accepted the answer of jrj.healey, although I believe that he solved your issue.
Well, I believed that it was very close and really helpful, but I have accepted it also. Thankyou for guiding me this way. :)
Glad to help and welcome to biostars. Interesting guidelines for posting can be found in the following posts:
Thankyou for this sharing..! Can I also get some help about my question I mentioned above.???