模块导入安装、编码格式、文件的读写、with语句、os模块、os.path模块

# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 19:50
# @Author  : 李新宇
# @FileName: demo1.py
# @Software: PyCharm

print('你好,中国')



# -*- coding: utf-8 -*-
# @Time    : 2021/10/3 20:07
# @Author  : 李新宇
# @FileName: demo2.py
# @Software: PyCharm



# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

file=open('a.txt','r')
print(file.readlines())
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

file=open('b.txt','w')
file.write('Python')
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

file=open('b.txt','a')
file.write('Python')
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

#以二进制方式读写
src_file=open('logo.png','rb')
target_file=open('copylogo.png','wb')
target_file.write(src_file.read())
target_file.close()
src_file.close()


# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

file=open('a.txt','r')
#print(file.read())
print('--------------------')
#print(file.readlines())
print('--------------')
print(file.readline())
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

file=open('a.txt','a')
#file.write('hello')
lst=['java','python','12']
file.writelines(lst)
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:15
# @Author  : 李新宇
# @FileName: demo3.py
# @Software: PyCharm

file=open('a.txt','r')
file.seek(2)
print(file.read())
print(file.tell())
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:41
# @Author  : 李新宇
# @FileName: demo10.py
# @Software: PyCharm
file=open('c.txt','a')
file.write('hello')
file.flush()
file.write('world')
file.close()
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:41
# @Author  : 李新宇
# @FileName: demo10.py
# @Software: PyCharm
file=open('c.txt','a')
file.write('hello')
file.close()
# file.write('world')
# file.flush()

# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:46
# @Author  : 李新宇
# @FileName: demo12.py
# @Software: PyCharm


with open('a.txt','r') as file:
    print(file.read())

# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 9:52
# @Author  : 李新宇
# @FileName: demo13.py
# @Software: PyCharm

'''
MyContentMgr实现了特殊方法__enter__ ,__exut__()称为该对象遵守了上下管理器协议
该类对象的实例对象,称为上下文管理器
'''
class MyContentMgr(object):
    def __enter__(self):
        print('enter方法被调用执行了')
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        print('exit方法被调用执行了')

    def show(self):
        print('show方法被调用执行了')

with MyContentMgr() as file: #相当于file=MyContentMgr
    file.show()



# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 10:06
# @Author  : 李新宇
# @FileName: demo14.py
# @Software: PyCharm

with open('log.png','rb') as src_file:
    with open('copy2logy.png','wb') as target_file:
        target_file.write(src_file.read())


# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 10:09
# @Author  : 李新宇
# @FileName: demo15.py
# @Software: PyCharm

#os模块与操作系统相关的模块
import os
# os.system('notepad.exe')
# os.system('calc.exe')
#直接调用可执行文件
#os.startfile('C:\\tencent\\Bin\\QQScLauncher.exe')

# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 10:14
# @Author  : 李新宇
# @FileName: demo16.py
# @Software: PyCharm
import os
print(os.getcwd())
lst=os.listdir('../chap15')
print(lst)



#os.mkdir('newdir2')
#os.makedirs('A/B/C')
#os.rmdir('newdir2')
#os.removedirs('A/B/C')

# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 10:22
# @Author  : 李新宇
# @FileName: demo17.py
# @Software: PyCharm

import os.path
print(os.path.abspath('demo13.py'))
print(os.path.exists('demo13.py'),os.path.exists('demo18.py'))
print(os.path.join('E:\\Python','demo13.py'))
print(os.path.split('E:\\vippython\\chap15\\demo13.py'))
print(os.path.splitext('demo13.py'))
print(os.path.basename('E:\\vippython\\chap15\\demo13.py'))
print(os.path.dirname('E:\\vippython\\chap15\\demo13.py'))
print(os.path.isdir('E:\\vippython\\chap15\\demo13.py'))
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 10:31
# @Author  : 李新宇
# @FileName: demo18.py
# @Software: PyCharm

#列出指定目录下的所有py文件
import os
path=os.getcwd()
lst=os.listdir(path)
for filename in lst:
    if filename.endswith('.py'):
        print(filename)
# -*- coding: utf-8 -*-
# @Time    : 2021/10/4 10:34
# @Author  : 李新宇
# @FileName: demo19.py
# @Software: PyCharm

import os
path=os.getcwd()
lst_files=os.walk(path)
#print(lst_files)  #<generator object _walk at 0x000001DCB0EF9AC0>
for dirpath,dirname,filename in lst_files:
   ''' print(dirpath)
    print(dirname)
    print(filename)
    print('-------------------')'''
   for dir in dirname:
       print(os.path.join(dirpath,dir))
       print('----------------------------------')
   for file in filename:
       print(os.path.join(dirpath,file))
print('----------------------------------')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橙黄橘绿时_Eden

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值