【python地图】基础4文件输入输出

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:albert time:2018/12/27

import os
import shutil
# 导入os,os属于内置模块,安装好python后就可以直接使用
# help(os)

# 1 os.access(path, mode) 用于判断给的path是否有mode
# mode:
# os.F_OK  --  path是否存在
# os.R_OK  --  path是否可读
# os.W_OK  --  path是否可写
# os.X_OK  --  path是否可执行

# print(os.access(os.curdir+'/aaa',os.F_OK))
# print(os.access(os.curdir,os.R_OK))
# print(os.abort())
# print(os.access(os.curdir,os.W_OK))
# print(os.access(os.curdir,os.X_OK))

# 2  os.chdir(path)
# print(os.getcwd())
# os.chdir('E:\\00_Project')
# print(os.getcwd())

# 3  os.close(fd)
# fd = os.open("foo.txt", os.O_RDWR|os.O_CREAT)
# os.close(fd)
# os.remove('E:\\00_Project\\test-tech\\python学习\\Module\\Inner\\foo.txt')

# 4  os.walk()
# path = 'E:\\00_Project\\test-tech\\python学习\\Module'
# file_list = []
# dir_list = []
# for root, dirs, files in os.walk(path):
#     for file in files:
#         file_list.append(os.path.join(path,file))
#     for dir in dirs:
#         dir_list.append(os.path.join(path,dir))
# print(file_list)
# print(dir_list)

# 对比
# temp_file_list = []
# list_dirs = []
# list_files = []
#
# def list_all_files(path):
#     if os.access(path,os.F_OK) and os.path.isdir(path):
#         temp_file_list = os.listdir(path)
#         for temp_file in temp_file_list:
#             abs_temp_file = os.path.join(path,temp_file)
#             if os.path.isfile(abs_temp_file):
#                 list_files.append(abs_temp_file)
#             if os.path.isdir(abs_temp_file):
#                 list_dirs.append(abs_temp_file)
#                 list_all_files(abs_temp_file)
#
# list_all_files(path)
# print(list_dirs)
# print(list_files)

# 一、文件的读写
path = 'E:\\00_Project\\test-tech\\python学习'
file_path = 'E:\\00_Project\\test-tech\\python学习\\Module\\Inner\\test.txt'
# 文件打开方式:r  w   a   rb  wb  ab  r+  w+  a+  rb+ wb+ ab+
# r: 只读  w:写  a:追加  b:二进制(组合,不能单独使用)  +:读写模式(组合,不能单独使用)
# r/rb/r+/rb+ 模式文件必须要存在,否则会报没有文件,其他模式如果文件不存在,则会自动创建文件
# ① 常用的文件读写    注意点: r+、w+、a+  相同点:文件打开方式都为读写模式;差异:r+ 文件必须存在,光标在最开始,w+ 会删掉原内容,光标在最后,a+ 在原内容上追加内容,光标在最后。
# with open(file_path, 'a+', encoding='utf-8') as file_handle:
#     file_handle.write('222\n')
#     file_handle.seek(0)
#     file_content = file_handle.read()           # 读光标后的所有内容,字符串返回
#     print(file_content)
#     file_handle.seek(33)
#     file_content2 = file_handle.readline()      # 读光标所在的一行,字符串返回
#     print(file_content2)
#     print(file_handle.tell())                   #光标现在所在位置
#     file_handle.seek(0)                         #移动光标位置
#     file_content3 = file_handle.readlines()     # 读光标之后的行,每行为列表中一个元素,返回列表
#     print(file_content3)

# 二、常用方法
# ① 当前目录
print(os.getcwd())
os.chdir('E:\\00_Project\\test-tech\\python学习\\Module')
print(os.getcwd())
print(os.path.abspath('.'))
print(os.path.basename(r'E:\\00_Project\\test-tech\\python学习\\Module'))
print(os.path.split(r'E:\\00_Project\\test-tech\\python学习\\Module'))

# ② 检查路径
print(os.path.isfile('E:\\00_Project\\test-tech\\python学习\\Module'))
print(os.path.isdir('E:\\00_Project\\test-tech\\python学习\\Module'))
print(os.path.exists('E:\\00_Project\\test-tech\\python学习\\Module'))

# ③ 创建文件夹
if os.path.exists('E:\\00_Project\\test-tech\\python学习\\Module\\A\\B'):
    pass
else:
    os.makedirs('E:\\00_Project\\test-tech\\python学习\\Module\\A\\B')

# ④ 连接路径
print(os.path.join('E:\\00_Project\\test-tech\\python学习\\Module','aaaa.txt'))

# ⑤ 遍历文件
os.walk(path)

# ⑥ 删除文件/文件夹
shutil.rmtree('E:\\00_Project\\test-tech\\python学习\\Module\\A')

# ⑦ 重命名
# os.rename()
# os.replace()

# ⑧ 获取文件大小
print(os.path.getsize(r'E:\00_Project\test-tech\python学习\Module\Inner\test.txt'))

# ⑨ 复制文件或文件夹
# shutil.copy()
# shutil.copy2()
# shutil.copyfile()

# ⑩ 移动文件及文件夹
# shutil.move()













  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值