line or string? [closed]
I've used different ways for INPUT these days using python.
f=open('txt','r')
for line in f:.....
f=open ('txt','r').readlines()
for line in f:...
samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
stdout=subprocess.PIPE,bufsize=1)
f = samtoolsin.stdout
for line in f:.....
f= commands.output('zcat '+ file)
for line in f:.....
For all the situations above, is f a list, or just string? According to my experience, seems 1,2,3 are all list, but the 4th is string. But I don't know why.thx
Short Answer:
- 1 and 3: file object
- 2: list
- 4: string
Long Answer (mainly links to docs):
f=open('txt','r')
- open() returns a File Object. It is iterable, but is neither a string nor a list.
f=open ('txt','r').readlines()
- file.readlines() returns a list
samtoolsin = subprocess.Popen(..., stderr=subprocess.PIPE, ...)
f = samtoolsin.stdout
- subprocess.Popen.stdout will be either a File Object (as in your case) or
None
.
f= commands.output('zcat '+ file)
- commands.getoutput() returns a string.
When iterating over a file object f is actually an iterator that returns one line each time it's called. This has lots of advantages because if you do .readlines() you store the entire list in memory, and also have to read the whole thing before processing.
精彩评论