新建python的第一个小项目

环境上一章已经搭建好了,现在新建项目并编写第一个小demo

第一步:创建项目:

导航栏:file > new  > other  >pyDev >PyDev project


第二步:编写小demo

1:读取桌面txt中的内容

代码:


file_path = 'C:\\Users\\Lenovo\\Desktop\\test.txt'


with open(file_path) as file_object:
    contents = file_object.read()
    print(contents)


run一下  text文件中的内容输出到控制台。至此python读取文件小demo完成。

2:写入txt文件转中内容

fileName = 'C:\\Users\\Lenovo\\Desktop\\writeFile.txt'

with open(fileName,'w') as file_object:
    file_object.write("我是来写文件的")

3:处理一个异常

try:
    print(5/0)
except ZeroDivisionError:
    print("发生异常了")

4:for循环

squares = [value**2 for value in range(1,11)]
print(squares)

之前一直编写java代码,从学习python后感觉代码量减少了太多了,java处理流较为繁琐,python自动选择合适的时机关闭流。

猜你喜欢

转载自blog.csdn.net/goodlook0123/article/details/80008848