由于某种原因,我无法显示此文件

时间:2019-11-06 23:37:54

标签: python image printing

我要显示的文件是普通文本文件。

这是我到目前为止编写的代码:

import os                      

THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
my_file = os.path.join(THIS_FOLDER, r' C:\Users\dylan\PycharmProjects\Crooms terminal\ Welcome to the crooms terminal.txt')

for name in my_file:
    with open(name, "r", encoding="utf8") as f:
        frames.append(f.readlines())

print(my_file)
for frame in frames:
    print("".join(frame))

这是我要打印的图像

  _____                                 _______                  _             _ 
 / ____|                               |__   __|                (_)           | |
| |     _ __ ___   ___  _ __ ___  ___     | | ___ _ __ _ __ ___  _ _ __   __ _| |
| |    | '__/ _ \ / _ \| '_ ` _ \/ __|    | |/ _ \ '__| '_ ` _ \| | '_ \ / _` | |
| |____| | | (_) | (_) | | | | | \__ \    | |  __/ |  | | | | | | | | | | (_| | |
 \_____|_|  \___/ \___/|_| |_| |_|___/    |_|\___|_|  |_| |_| |_|_|_| |_|\__,_|_|

1 个答案:

答案 0 :(得分:0)

您粘贴的代码格式不正确,我发现很多可能是错误的...

例如framesfile未定义

路径中有一些多余的空格,...

但是这是我要怎么做:

#THIS_FOLDER = os.path.dirname(os.path.abspath(file))
my_file = os.path.join(r"d:\temp", 'terminal.txt')
frames = []
#for name in my_file:
with open(my_file, "r") as f:
    frames.append(f.readlines())
print(my_file)
for frame in frames:
    print("".join(frame))

另外,显示文件内容的一种非常简单的方法是:

my_file = os.path.join(r"d:\temp", 'terminal.txt')
with open(my_file, "r") as f:
    print(f.read())