将python结果打印到txt文件

时间:2019-06-22 22:45:02

标签: python printing

我正在尝试将程序结果打印到用户命名的文件中。 我的程序创建了文件,但未写入文件。我的代码正在按我的方式工作,只是打印有问题。我需要随机文件字符串才能打印到界面用户命名的文件。

from random import randrange
from random import choice


def randomShape():
    x = randrange (0,400)
    y = randrange (0,400)
    x2 = randrange(0,400)
    y2 = randrange(0,400)

    radius = randrange (0,100)
    red = randrange (192, 208)
    blue = randrange(100,140)
    green = randrange(150,175)
    shape = ['cirle;','rectangle;']
    randomShape = choice (shape)

    if randomShape == 'cirle;':
        print(randomShape,x,",",y,";",radius,";",red,",",blue,",",green)

    elif randomShape != 'cirle;':
        print(randomShape,x,",",y,";",x2,",",y2,";",red,",",blue,",",green)


def getColor( colorString ):
    tokens = colorString.split(',')
    if len( tokens ) == 3:
        return color_rgb( int( tokens[0] ), int(tokens[1]), int( tokens[2] ))
    elif len( colorString ) > 0:
       return colorString.strip()
    else:
        return 'white'



def main():
    kFile = input("Enter the drawing file name to create:")
    openfile = open(kFile,"w")

    q = int(input("Enter the number of shapes to make:"))

    for x in range(q):
        print (randomShape(),kFile)



main()

3 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

from random import randrange
from random import choice
from graphics import color_rgb

def randomShape():
    x = randrange (0,400)
    y = randrange (0,400)
    x2 = randrange(0,400)
    y2 = randrange(0,400)

    radius = randrange (0,100)
    red = randrange (192, 208)
    blue = randrange(100,140)
    green = randrange(150,175)
    shape = ['cirle;','rectangle;']
    randomShape = choice(shape)

    if randomShape == 'cirle;':
        print(randomShape,x,",",y,";",radius,";",red,",",blue,",",green)

    elif randomShape != 'cirle;':
        print(randomShape,x,",",y,";",x2,",",y2,";",red,",",blue,",",green)
    return randomShape


def getColor( colorString ):
    tokens = colorString.split(',')
    if len( tokens ) == 3:
        return color_rgb( int( tokens[0] ), int(tokens[1]), int( tokens[2] ))
    elif len( colorString ) > 0:
       return colorString.strip()
    else:
        return 'white'

def main():
    kFile = input("Enter the drawing file name to create:")
    q = int(input("Enter the number of shapes to make:"))

    with open(kFile, "w") as file:
        for x in range(q):
            file.write(randomShape())

main()

在上面的代码中,您必须从randomShape返回randomShape()的值并将输出写入文件。而且,这里需要注意的另一件事是,您应该考虑使用open(kFile, "a")之类的追加模式,以保持追加到现有文件中,并且在每次运行该函数时都不会覆盖它。

答案 1 :(得分:0)

print (randomShape(),kFile)

这不会写入文件。 使用以下命令打开文件后:

openfile = open(kFile,"w")

您现在在openFile中有一个文件句柄。您可以使用此方法将数据写出(并确保也关闭文件)。一般来说,您可以使用write方法-https://docs.python.org/3/library/io.html#io.TextIOBase.write

因此,在您的情况下,您想使用类似 kFile.write(randomShape())

另外,您的randomShape()函数也不会向函数调用返回任何数据。因此,您可能需要修改它,以使用return代替print

print(randomShape,x,",",y,";",radius,";",red,",",blue,",",green)print(randomShape,x,",",y,";",x2,",",y2,";",red,",",blue,",",green)

答案 2 :(得分:0)

通常推荐的方法是使用with语句。正如Mark Meyer所指出的,您还需要写入文件对象,而不是使用其名称来字符串。
结合这两种信息,尝试像这样:

filename = input("Filename: ")
with open(filename, "w") as openedFile:
    print("Some data", file=openedFile)