Python关于文件的管理及操作

######文件的管理及操作######

 

 

##文件的读取

 

1.文件读取的三部曲:打开 --- 操作 ---- 关闭

# 打开文件
f = open('/mnt/passwd', 'a+')
print(f)
## 操作文件:读
f.seek(0, 0)
print(f.read())
##操作文件:写
f.write('hello python'.center(50, '*'))
print(f.read())
# 判断文件对象拥有的权限,返回值是bool类型
# print(f.readable())  # True
# print(f.writable())  # False
# 关闭文件
f.close()

 

# 2. 文件读取模式的选择
r:
    - 只能读, 不能写;
    - 读取的文件不存在, 是否会报错;
    FileNotFoundError: [Errno 2] No such file or directory: '/etc/passwd1'
r+:
    - 可以执行读写操作;
    - 文件不存在, 报错;
    - 默认情况下, 从文件指针所在位置开始写入;

w:
    - write only
    - 文件不存在, 不报错
    - 会清空文件内容
w+:
    - rw
    - 文件不存在, 不报错
    - 会清空文件内容
a:
    - write only
    - 文件不存在, 不报错
    - 不会清空文件内容
a+:
    - rw
    - 文件不存在, 不报错
    - 不会清空文件内容

f = open('/mnt/hellopython', 'a+')
print(f.write('hellopython'))  # 写入的返回值是内容的长度
f.seek(0, 0)
print(f.read())
f.close()

 

k = open('/mnt/hellopython', 'w+')
print(k.write('kobebryant'))
k.seek(0,0)
print(k.read())
k.close()

 

举例说明:

# 1. 文件操作
#   1). 创建文件data.txt, 文件共100000行, 每行存放一个1~100之间的整数.
#   2). 找出文件中数字出现次数最多的10个数字, 写入文件mostNum.txt;

1)

import random

f = open('data.txt', 'w+')
for i in range(100000):
    f.write(str(random.randint(1,100)) + '\n')
print(f.read())
f.close()

2)

from collections import Counter
from collections import Iterable
dict={}
f = open('data.txt', 'r+')
print(isinstance(f, Iterable))
for i in f:
    if i not in dict:
        dict[i] = 1
    else:
        dict[i] = dict[i] + 1
d = Counter(dict)
with open('mostNum.txt', 'w+') as k:
    for i in d.most_common(10):
        k.write(i[0].strip())
    k.seek(0, 0)
    print(k.read())
f.close()

注:

先判断这个TXT文件是否为可迭代对象;

之后利用Counter类里的most_common方法来直接比较字典的value值取value值最大的前十个元素。

 

# 2. 添加行号
#   1). 编写程序, 将/tmp/passwd 操作生成文件/tmp/passwdNum文件,
#   2). 其中文件内容与/tmp/passwd一致, 但是在每行的行尾加上行号。

with open('/tmp/passwd', 'r+') as f1, open('/tmp/passwdNum', 'w+') as f2:
    for i, j in enumerate(f1):
        f2.write(j.strip()+str(i)+'\n')
    f2.seek(0, 0)
    print(f2.read())

注:

利用with语句我们可以同时打开多个文件。

 

# 非文本文件的读取:

# 如果读取图片, 音乐或者视频(非文本文件), 需要通过二进制的方式进行读取与写入;b

- 读取二进制文件
rb:rb+:wb:wb+:ab:ab+:
- 读取文本文件
rt:rt+:wt:wt+:at:at+  等价于  r:r+:w:w+:a:a+

# 先读取二进制文件内容, 保存在变量content里面
f1 = open("frame1.png", mode='rb')
content = f1.read()
f1.close()
# print(content)

f2 = open('hello.png', mode='wb')
f2.write(content)
f2.close()

 

# 文件的属性:

f = open('/tmp/passwd')
"""
f.buffer
<_io.BufferedReader name='/etc/passwd'>
bufferObj = f.buffer
from collections import  Iterable
isinstance(bufferObj, Iterable)
True
for i in bufferObj:
    print(i)
    
f.close()
# 当只是读取文件时, buffer里面没有内容
f = open("/tmp/passwd", 'a+')
f.read()
''
f.write('123')
3
# 此时写入的内容在缓冲区里面,并没有真正写入磁盘文件
f.buffer
<_io.BufferedRandom name='/tmp/passwd'>
for i in f.buffer:
    print(i, end=',')
    
f.close()

"""
# buffer缓冲区对象
print(f.buffer)

# 判断文件对象是否关闭, 已经关闭返回True
print(f.closed)

# 文件号
print(f.fileno())
# 文件对象
print(f.name)

# 查看文件对象的相关信息
print(f)

 

# 读取文件内容的方法:
f = open('/tmp/passwd', 'r+')
# f.read():
#       默认情况读取文件的所有内容, 小的文件, 直接用read读取即可,
#       如果是一个大文件时(文件大小>内存大小), 不能通过read一次性读取所有内容;
# f.readline(): 每次读取一行内容
# f.readlines()  #查看全部行
# f.readable()  #是否可读

# # 类似于head -c 3 /tmp/passwd命令, 自己写一个和阿的命令
# print(f.read(3))     #查看指针所在位置的后三个字符

# print(f.readline(), end='')
# print(f.readline(), end='')
# print(f.readline(), end='')

# 读取文件的所有内容, 返回一个列表, 列表元素分别为文件行内容
# print(f.readlines())

f = open('/tmp/passwd', 'r+')
print(f.read(3))
f.seek(0, 0)
print(f.readline())
print(f.readline())
print(f.readline())
f.seek(0, 0)
print(f.readlines())
print(f.readable())

 

# 对于每一行, 去掉后面的'\n' --(列表生成式, map)
# print([line.strip() for line in f.readlines()])
# print(list(map(lambda  x: x.strip(), f.readlines())))

# # 文件的写入操作
# # f.write() :              #从指针所在位置写入, 写入是字符串内容
# # f.writelines():    #将列表里面的每个元素写入文件中
# # f.writable()           #文件是否可写
# li = ['user'+str(i)+'\n' for i in range(10)]
# f.writelines(li)

li= ['user'+str(i)+'\n' for i in range(10)]
with open('data.txt', 'w+') as f:
    f.writelines(li)
    f.seek(0,0)
    print(f.read())

# 指针位置的操作
# 告诉当前指针所在位置
print(f.tell())

# 将文件指针移动到文件最开始
# f.seek(0, 0)
# 将指针移动到文件最后
f.seek(0,2)

 

## 文件安全读取(with语句)
 

# 上下文管理器:打开文件, 执行完with语句内容之后, 自动关闭文件对象

  with open('/tmp/passwd') as f: 不用再次执行f.close()

# 同时打开两个文件对象(这种写法, python2中不支持)
   with  open('/tmp/passwd') as f1,  open('/tmp/passwdBack', 'w+') as f2:
    # 将第一个文件的内容写入第二个文件中;
    f2.write(f1.read())
    # 移动指针移动到文件最开始
    f2.seek(0,0)
    # 读取指针内容
    print(f2.read())

 

## yield 实现读取大文件

# import random
#
# with open('data.txt', mode='a+') as f:
#     for i in range(1000000):
#         f.write(str(random.randint(1,100))+'\n')


# # 通过yield, 每次读取一行进行处理
# def byLineReader(filename):
#     with open(filename) as f:
#         line = f.readline()
#         # 如果可以读取到内容, 返回改行信息
#         while line:
#             yield  line
#             line = f.readline()
#
# # read是一个生成器对象,
# read = byLineReader('data.txt')
# print(read)
#
# # #1). next 读取生成器的内容
# # print(next(read))
# # print(next(read))

def lines(filename):
    with open(filename) as f:
        line = f.readline()
        while line:
            yield line
            line = f.readline()

read = lines('yield.txt')
print(read)
print(next(read))
print(next(read))
print(next(read))
print(next(read))

# 通过for循环
# for item in read:
#     print(item)


# ******** 文件对象是可以for循环遍历的, 默认遍历的内容为每一行的内容.是节省内存空间的。
from collections import Iterable

f = open('yield.txt')
print(isinstance(f, Iterable))
for i, item in enumerate(f):
    if i == 10:
        break
    print(i, item)

 

 

 

 

## os模块

 

#### os之环境变量函数

import os

 1). 返回操作系统类型, 值为posix,是Linux操作系统, 值为nt, 是windows操作系统
print(os.name)
print('Linux' if os.name=='posix' else 'Windows')

 2). 操作系统的详细信息
info = os.uname()
print(info)                                ##打印操作系统全部信息
print(info.sysname)              ##打印系统名称

print(info.nodename)           ##打印主机名称

 3). 系统环境变量
print(os.environ)

 4). 通过key值获取环境变量对应的value值
print(os.environ.get('PATH'))
print(os.getenv('PATH'))

 

##### os之文件操作函数

 

 

import os
import random

from os.path import exists, splitext, join

# # -判断绝对路径

# print(os.path.isabs('/tmp/hello'))
# print(os.path.isabs('hello'))
#
# # -生成绝对路径
# print(os.path.abspath('/tmp/hello'))
# print(os.path.abspath('hello.png'))

 

import os
import random
from os.path import exists, splitext, join
print(os.path.isabs('/tmp/passwd'))
print(os.path.isabs('passwd'))

print(os.path.abspath('tmp/passwd'))
print(os.path.abspath('passwd.png'))


# #  -创建绝对路径
# print(os.path.join('/home/kiosk', 'hello.png'))
# # 返回一个绝对路径: 当前目录的绝对路径+ 文件名/目录名
# print(os.path.join(os.path.abspath('.'), 'hello.png'))
#
#


# # - 获取目录名或者文件名
# filename = '/home/kiosk/Desktop/file'
# print(os.path.basename(filename))     #目录
# print(os.path.dirname(filename))         #文件


# #- 创建目录/删除目录
# mkdir -p qq/qq1/qq1   递归创建目录
# os.makedirs('img/films')
# os.mkdir('img')
# os.rmdir('img')

# #-. 创建文件/删除文件
# os.mknod('00_ok.txt')
# os.remove('00_ok.txt')

# #.- 文件重命名(mv)
# os.rename('data.txt', 'data1.txt')


# # -. 判断文件或者目录是否存在
# print(os.path.exists('img'))
#
# # -. 分离后缀名和文件名
# print(os.path.splitext('hello.png'))
# print(os.path.split('hello.png'))
#
# # -. 将目录名和文件名分离
# print(os.path.split('/tmp/hello/hello.png'))

 

 

 

 

#########################################

 

 

 

 

 

 

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值