1

I'm trying to write a script that asks for an input file and then runs some command on it. when I run the script it askes me for filename and when I give the file (e.g example.bam) then I get this error:

NameError: name 'example.bam' is not defined

I tried many things but I couldn't fix it. Can someone tell me what is wrong?

This is my comand:

from subprocess import call
filename = input ("filename: ");
with open (filename, "r") as a:
    for command in ("samtools tview 'a' /chicken/chick_build2.1_unmasked.fa",):
        call(command, shell=True)

This is a short version of my command: it has to do much more stuff. I'm also thinking to input 4-6 files at same time (perhaps this information is helpful to clarify my intentions).

3
  • I should add that the file that I'm trying to input is in BAM format (a binary format) Commented Oct 27, 2011 at 11:26
  • The error you posted gives the impression that you are using the example variable somewhere, but it is not yet defined (in the part you posted, at least). Do you have any idea where that variable is used, and if so, could you post it? Commented Oct 27, 2011 at 11:28
  • the "example" is the file that I put for input! so when I run this script it ask me for a file and then I put example.bam and then it gives me the error that NameError: name 'example.bam' is not defined Commented Oct 27, 2011 at 11:33

1 Answer 1

3

input is equivalent to eval(raw_input(prompt)). So what your script currently tries to do is interpret your input ("example", in your case), and execute as if it were a statement in your script. For user input (and might I simply say "for any input" -- unless you know what you're doing), always use the raw_input function.

So, to solve it, replace input with raw_input:

filename = raw_input("filename: ")
Sign up to request clarification or add additional context in comments.

10 Comments

now I get another error that the file dose not exist! > open: No such file or directory
Two things to check: is the filename you entered correct? Is the file available on the current path?
both of the answer is yes! it's correct and it's in the same folder so I just put the name! and I even tried it with the whole path from the root!
I'm absolutely certain that this: filename = raw_input("Filename: ")\nwith open(filename, 'r') as f:\n for line in f.readlines():\n print line.strip() (replace \n with newlines) will print the contents of a file in the current directory. Are you sure that the files you run with the call function aren't the culprit?
I run this script that you told me now with my file and I got:\n with open (Filename, 'r') as f:\n NameError: name 'Filename' is not defined\n and at first I thought it's the same problem (that the call function are the culprit) but the I call the same command and I just replace the "a" with file name! and it works perfectly! can it be because it's a binary file!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.