Loo*_*ast 151 python file-writing
我想使用python将打印重定向到.txt文件.我有一个'for'循环,它会'打印'我的每个.bam文件的输出,而我想将所有这些输出重定向到一个文件.所以我试着把
f = open('output.txt','w'); sys.stdout = f
Run Code Online (Sandbox Code Playgroud)
在我的脚本开头.但是我在.txt文件中什么都没得到.我的脚本是:
#!/usr/bin/python
import os,sys
import subprocess
import glob
from os import path
f = open('output.txt','w')
sys.stdout = f
path= '/home/xug/nearline/bamfiles'
bamfiles = glob.glob(path + '/*.bam')
for bamfile in bamfiles:
filename = bamfile.split('/')[-1]
print 'Filename:', filename
samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
stdout=subprocess.PIPE,bufsize=1)
linelist= samtoolsin.stdout.readlines()
print 'Readlines finished!'
........print....
........print....
Run Code Online (Sandbox Code Playgroud)
所以有什么问题?除了这个sys.stdout之外的任何其他方式?
我需要我的结果如下:
Filename: ERR001268.bam
Readlines finished!
Mean: 233
SD: 10
Interval is: (213, 252)
Run Code Online (Sandbox Code Playgroud)
Gri*_*ave 224
最明显的方法是打印到文件对象:
with open('out.txt', 'w') as f:
print >> f, 'Filename:', filename # Python 2.x
print('Filename:', filename, file=f) # Python 3.x
Run Code Online (Sandbox Code Playgroud)
但是,重定向stdout对我也有用.这对于像这样的一次性脚本来说可能很好:
import sys
orig_stdout = sys.stdout
f = open('out.txt', 'w')
sys.stdout = f
for i in range(2):
print 'i = ', i
sys.stdout = orig_stdout
f.close()
Run Code Online (Sandbox Code Playgroud)
从shell本身重定向是另一个好的选择:
./script.py > out.txt
Run Code Online (Sandbox Code Playgroud)
其他问题:
脚本中的第一个文件名是什么?我没有看到它初始化.
我的第一个猜测是glob没有找到任何bamfiles,因此for循环不会运行.检查文件夹是否存在,并在脚本中打印出bamfiles.
另外,使用os.path.join和os.path.basename来操作路径和文件名.
agf*_*agf 64
您可以使用>>
操作员重定向打印.
f = open(filename,'w')
print >>f, 'whatever' # Python 2.x
print('whatever', file=f) # Python 3.x
Run Code Online (Sandbox Code Playgroud)
在大多数情况下,您最好只是正常写入文件.
f.write('whatever')
Run Code Online (Sandbox Code Playgroud)
或者,如果你想要用几个空格写下几个项目,例如print
:
f.write(' '.join(('whatever', str(var2), 'etc')))
Run Code Online (Sandbox Code Playgroud)
小智 30
这非常有效:
import sys
sys.stdout=open("test.txt","w")
print ("hello")
sys.stdout.close()
Run Code Online (Sandbox Code Playgroud)
现在hello将被写入test.txt文件.确保关闭stdout
a close
,没有它,内容将不会保存在文件中
Yeo*_*Yeo 30
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
的文件参数必须是与对象
write(string)
方法; 如果它不存在或None
,sys.stdout
将被使用.由于打印的参数转换为文本字符串,print()
因此不能与二进制模式文件对象一起使用.对于这些,请file.write(...)
改用.
由于文件对象通常包含write()
方法,因此您需要做的就是将文件对象传递给其参数.
with open('file.txt', 'w') as f:
print('hello world', file=f)
Run Code Online (Sandbox Code Playgroud)
with open('file.txt', 'a') as f:
print('hello world', file=f)
Run Code Online (Sandbox Code Playgroud)
jpy*_*ams 18
print
,使用logging
您可以更改sys.stdout
为指向文件,但这是一种非常笨重且不灵活的方法来处理此问题.而不是使用print
,使用该logging
模块.
使用logging
,您可以像您一样打印stdout
,或者您也可以将输出写入文件.你甚至可以使用不同的消息级别(critical
,error
,warning
,info
,debug
),例如,只打印重大问题到控制台,但仍记录次要代码行动的文件.
导入logging
,获取logger
和设置处理级别:
import logging
logger = logging.getLogger()
logger.setLevel(logging.DEBUG) # process everything, even if everything isn't printed
Run Code Online (Sandbox Code Playgroud)
如果要打印到标准输出:
ch = logging.StreamHandler()
ch.setLevel(logging.INFO) # or any other level
logger.addHandler(ch)
Run Code Online (Sandbox Code Playgroud)
如果你还想写一个文件(如果你只想写一个文件跳过最后一节):
fh = logging.FileHandler('myLog.log')
fh.setLevel(logging.DEBUG) # or any level you want
logger.addHandler(fh)
Run Code Online (Sandbox Code Playgroud)
然后,无论您在哪里使用,都可以使用print
以下logger
方法之一:
# print(foo)
logger.debug(foo)
# print('finishing processing')
logger.info('finishing processing')
# print('Something may be wrong')
logger.warning('Something may be wrong')
# print('Something is going really bad')
logger.error('Something is going really bad')
Run Code Online (Sandbox Code Playgroud)
要了解有关使用更多高级logging
功能的更多信息,请阅读logging
Python文档中的优秀教程.
Aar*_*our 12
最简单的解决方案不是通过python; 它通过壳.从你的文件的第一行(#!/usr/bin/python
)我猜你是在UNIX系统上.只需print
像往常一样使用语句,并且不要在脚本中打开文件.当你去运行文件时,而不是
./script.py
Run Code Online (Sandbox Code Playgroud)
要运行该文件,请使用
./script.py > <filename>
Run Code Online (Sandbox Code Playgroud)
在哪里替换<filename>
您希望输出进入的文件的名称.该>
令牌告诉(最)弹到标准输出设置为以下令牌描述的文件.
这里需要提到的一件重要事情是"script.py"需要可执行./script.py
才能运行.
所以在运行之前./script.py
,执行此命令
chmod a+x script.py
(使脚本可执行所有用户)
yun*_*nus 10
如果您使用的是 Linux,我建议您使用该tee
命令。实现是这样的:
python python_file.py | tee any_file_name.txt
Run Code Online (Sandbox Code Playgroud)
如果您不想更改代码中的任何内容,我认为这可能是最好的解决方案。您也可以实现记录器,但您需要对代码进行一些更改。
您可能不喜欢这个答案,但我认为这是正确的答案。除非绝对必要,否则不要更改您的标准输出目的地(也许您正在使用仅输出到标准输出的库???在这里显然不是这种情况)。
我认为作为一个好习惯,您应该提前将数据准备为字符串,然后打开文件并立即写入整个内容。这是因为输入/输出操作打开文件句柄的时间越长,该文件发生错误的可能性就越大(文件锁定错误、I/O 错误等)。只需在一次操作中完成所有操作,就不会出现何时可能出错的问题。
下面是一个例子:
out_lines = []
for bamfile in bamfiles:
filename = bamfile.split('/')[-1]
out_lines.append('Filename: %s' % filename)
samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
stdout=subprocess.PIPE,bufsize=1)
linelist= samtoolsin.stdout.readlines()
print 'Readlines finished!'
out_lines.extend(linelist)
out_lines.append('\n')
Run Code Online (Sandbox Code Playgroud)
然后,当您完成收集每个列表项一行的“数据行”后,您可以将它们与一些'\n'
字符连接起来,使整个内容可输出;甚至可以将您的输出语句包装在一个with
块中,以提高安全性(即使出现问题,也会自动关闭您的输出句柄):
out_string = '\n'.join(out_lines)
out_filename = 'myfile.txt'
with open(out_filename, 'w') as outf:
outf.write(out_string)
print "YAY MY STDOUT IS UNTAINTED!!!"
Run Code Online (Sandbox Code Playgroud)
但是,如果您有大量数据要写入,则可以一次写入一个。我认为它与您的应用程序无关,但这是替代方案:
out_filename = 'myfile.txt'
outf = open(out_filename, 'w')
for bamfile in bamfiles:
filename = bamfile.split('/')[-1]
outf.write('Filename: %s' % filename)
samtoolsin = subprocess.Popen(["/share/bin/samtools/samtools","view",bamfile],
stdout=subprocess.PIPE,bufsize=1)
mydata = samtoolsin.stdout.read()
outf.write(mydata)
outf.close()
Run Code Online (Sandbox Code Playgroud)