文件的操作repeat

#_author:来童星
#date:2019/12/15
import os
#1
# print(os.name)# nt------>windows操作系统
#2 用于获取当前操作系统的换行符
# print(os.linesep)# 为什么不显示
# # 用于获取当前操作系统所使用的路径分隔符
# print(os.sep)#
#4.返回当前工作目录
# print(os.getcwd())# D:pycharm_file ew_fileslearn_pythonweek8day_7
# 5. 返回指定路径下的文件和目录信息
# print(os.listdir('week7\day1\day2')) !!!!有问题
# 6.
# os.mkdir('d:\ltx1')
#7.判断目录是否存在
# os.makedirs('d:\demo\test')
# path='d:\demo'
# if not os.path.exists(path):
# os.makedirs(path)
# print('目录创建成功')
# else:
# print('目录创建失败')
# 8 拼接路径
# print(os.path.abspath('dir_operator_2.py'))# D:pycharm_file ew_fileslearn_pythonweek8day_7dir_operator_2.py
# print(os.path.join('D:pycharm_file\new_fileslearn_python','week8\day_7\dir_operator_2.py'))
# D:pycharm_file ew_fileslearn_pythonweek8day_7dir_operator_2.py
# 9.删除目录
# import shutil# 删除不为空的母鹿
# path='e:\demo'
# if os.path.exists(path):
# shutil.rmtree(path)
# print('删除成功')
# else:
# print('删除失败')
#
# os.makedirs('E:\demo\test\a.txt')
# path='E:\demo'
# if os.path.exists(path):
# os.rmdir(path)
# print('删除成功')
# else:
# print('删除失败')
#10遍历文件
# tuples='D:\pycharm_file\new_files\learn_python\week7\day1'
# for item in os.walk(tuples,False):
# print(item)
#从上往下遍历
# print(os.walk(tuples))# <generator object walk at 0x00000289EA60D830>
# ('D:\pycharm_file\new_files\learn_python\week7\day1', ['day2', 'day5', 'DAY7'], ['t1.py', 't2.py', 't3.py', 't4.py', 't5.py', 't6.py', 'test1.py', 'test_1.py', 'test_2.py', 'test_3.py'])
# ('D:\pycharm_file\new_files\learn_python\week7\day1\day2', [], ['t1.py', 't2.py', 'test2.py', 'test3.py'])
# ('D:\pycharm_file\new_files\learn_python\week7\day1\day5', [], ['backtime_fun.py', 'chengyutk.py', 'kj_dattetime_.py', 'kj_print.py', 't1.py', 't2.py', 't3.py', 't4.py', 't5.py', 'wechat_fun.py'])
# ('D:\pycharm_file\new_files\learn_python\week7\day1\DAY7', [], [])
# 从下往上遍历(遍历最后一级别子目录)
# ('D:\pycharm_file\new_files\learn_python\week7\day1\day2', [], ['t1.py', 't2.py', 'test2.py', 'test3.py'])
# ('D:\pycharm_file\new_files\learn_python\week7\day1\day5', [], ['backtime_fun.py', 'chengyutk.py', 'kj_dattetime_.py', 'kj_print.py', 't1.py', 't2.py', 't3.py', 't4.py', 't5.py', 'wechat_fun.py'])
# ('D:\pycharm_file\new_files\learn_python\week7\day1\DAY7', [], [])
# ('D:\pycharm_file\new_files\learn_python\week7\day1', ['day2', 'day5', 'DAY7'], ['t1.py', 't2.py', 't3.py', 't4.py', 't5.py', 't6.py', 'test1.py', 'test_1.py', 'test_2.py', 'test_3.py'])
# 11---》高级文件操作

# from urllib.request import urlopen
# import gevent
# def f(url):
# print('get:%s'%url)
# resp=urlopen(url)
# date=resp.read()
# with open('message123.html', 'wb') as file:
# file.write(date)
# print('%d bytes Received From %s'%(len(date),url))
# f('http://www.taobao.com/')

# if os.path.exists('message123.html'):
# os.remove('message123.html')
# print('success')
# else:
# print('file')
# 12 重命名文件
# src='m123.txt'
# dst='message123.txt'
# if os.path.exists(src):
# print('文件存在')
# os.rename(src,dst)
# else:
# print('文件不存在')
#
# src='D:\pycharm_file\new_files\learn_python\week8\day2'
# dst='D:\pycharm_file\new_files\learn_python\week8\day_2'
# if os.path.exists(src):
# print('dir更新完毕')
# os.rename(src,dst)
# else:
# print('dir不存在')
#13.
if os.path.exists('message123.txt'):
fileinfo=os.stat('message123.txt')
print('文件完整路径',os.path.abspath('message123.txt'))# 文件完整路径 D:pycharm_file ew_fileslearn_pythonweek8day_7message123.txt
print('文件大小',fileinfo.st_size)#文件大小 43
print('最后一次修改时间',fileinfo.st_mtime)#最后一次修改时间 1576416534.7083335
原文地址:https://www.cnblogs.com/startl/p/12046079.html