pycharm总是没有输出结果只是Process finished with exit code 0

今天写了个python程序,代码中有print,但是执行完后,输出的结果就只有:

Process finished with exit code 0

并没有将我打印的内容打印出来。

后来参考资料,总结有2点可能的原因:

1)File→settings→project→project interpreter设置的解释器有问题(建议创建项目的时候选择现有的配置好的解释器,不要使用新的pycharm的虚拟解释器,如图。)

2)代码格式问题——缩进不对(检查一下代码的缩进,实在觉得没毛病,就把缩进都删掉,重新缩进。)

我的是2)导致的。不过后面想再复现这个问题,复现不了。附上修改后的代码吧。

 

 

 

附代码:

'''
@Desc1requests模块的方法;2)文件读、写
'''


import requests


class TestMain:
def test(self):
r = requests.get("http://www.baidu.com") # get请求百度首页
print("访问百度首页的状态码、cookie、登录状态:", r.status_code, r.cookies, r.ok) # 打印响应码
# get请求的响应内容写入到项目文件夹下的text.txt(若文件不存在则新建文件)
with open("text.txt", 'w', encoding='utf-8') as f:
f.write(r.text)
# 读取text.txt-读取部分行
with open('F:\/cs_auto\/untitled1\/text.txt') as file_open: # 完整目录等同于"text.txt"
print("以下只打印第一行:")
for i in range(1):
print(file_open.readline())
# 不需要file_open.close(),因为关键字with在我们不再需要使用文件的时候将其关闭。
# 读取text.txt-读取整个文件
with open('text.txt') as file_open:
print("以下打印文件的全部内容:")
print(file_open.read())
# 读取text.txt-按行读取文件内容
with open('text.txt') as file_open:
print("以下按行读取并打印文件的全部内容:")
for content in file_open:
print(content.rstrip()) # 不调用rstrip()函数的话,会在每行后面多打印一个空行


if __name__ == '__main__':
print("this is main method")
TestMain.test('self')
posted @ 2020-06-29 15:35  就一点点  阅读(25948)  评论(0编辑  收藏  举报