栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

模块导入安装、编码格式、文件的读写、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)  #
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('----------------------------------')
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/294379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号