开发者

line or string? [closed]

开发者 https://www.devze.com 2023-03-30 04:26 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. 开发者_如何学编程 Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消