Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何实现基于参数输入的标准输出和文件写入_Python_File_Stdout - Fatal编程技术网

Python 如何实现基于参数输入的标准输出和文件写入

Python 如何实现基于参数输入的标准输出和文件写入,python,file,stdout,Python,File,Stdout,我的输入文件看起来像(infle.txt): 我想实现一个程序,允许用户根据命令写入标准输出或文件: python mycode.py infile.txt outfile.txt 将写入文件 用这个 python mycode.py infile.txt #2nd case 我会写信给STDOUT 我被以下代码困住了: import sys import csv nof_args = len(sys.argv) infile = sys.argv[1] print nof_args

我的输入文件看起来像(infle.txt):

我想实现一个程序,允许用户根据命令写入标准输出或文件:

python mycode.py infile.txt outfile.txt
将写入文件

用这个

python mycode.py infile.txt #2nd case
我会写信给STDOUT

我被以下代码困住了:

import sys
import csv

nof_args = len(sys.argv)
infile  = sys.argv[1]

print nof_args
outfile = ''
if nof_args == 3:
    outfile = sys.argv[2]

# for some reason infile is so large
# so we can't save it to data structure (e.g. list) for further processing
with open(infile, 'rU') as tsvfile:
    tabreader = csv.reader(tsvfile, delimiter=' ')

    with open(outfile, 'w') as file:
        for line in tabreader:
            outline = "__".join(line)
            # and more processing
            if nof_args == 3:
                file.write(outline + "\n")
            else:
                print outline
    file.close()
当使用第二个案例时,它会产生

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    with open(outfile, 'w') as file:
IOError: [Errno 2] No such file or directory: ''
回溯(最近一次呼叫最后一次):
文件“test.py”,第18行,在
打开(输出文件“w”)作为文件:
IOError:[Errno 2]没有这样的文件或目录:“”
实现它的更好方法是什么?

您可以尝试以下方法:

import sys

if write_to_file:
    out = open(file_name, 'w')
else:
    out = sys.stdout

# or a one-liner:
# out = open(file_name, 'w') if write_to_file else sys.stdout

for stuff in data:
    out.write(stuff)

out.flush() # cannot close stdout

# Python deals with open files automatically
您也可以使用它来代替
out.flush()

这看起来有点难看,所以,
flush
就可以了

您可以尝试以下方法:

import sys

if write_to_file:
    out = open(file_name, 'w')
else:
    out = sys.stdout

# or a one-liner:
# out = open(file_name, 'w') if write_to_file else sys.stdout

for stuff in data:
    out.write(stuff)

out.flush() # cannot close stdout

# Python deals with open files automatically
您也可以使用它来代替
out.flush()


这看起来有点难看,所以,
flush
就可以了

nof_args==2时,您是否将
outfile
定义为
stdout
?否则,以open(outfile,'w')作为文件的
将失败。
我看到您在删除的问题中使用了我的
len(sys.argv)
。有了答案后,您是否养成了删除问题的习惯?当
nof_args==2
时,您是否将
outfile
定义为
stdout
?否则,以open(outfile,'w')作为文件的
将失败。
我看到您在删除的问题中使用了我的
len(sys.argv)
。你有一个习惯,一旦你有了答案,就删除问题吗?你能用我的例子详细说明一下循环和
sys.argv
?@neversaint,只要调用
输出就行了。在循环中写入
。@neversaint,方法几乎相同:打开文件阅读或使用
sys.stdin
,但是不要
flush
,使用答案中提供的第二种方法尝试关闭文件。你也应该用
write
代替
read
。你能用我的例子详细说明一下循环和
sys.argv
吗?@neversaint,只要把对
的任何调用都放出来。write
放在循环里面。@neversaint,方法几乎相同:打开文件阅读或使用
sys.stdin
,但是不要
flush
,使用答案中提供的第二种方法尝试关闭文件。您还应该用
write
代替
read
try:
    out.close()
except AttributeError:
    pass