python面向对象操作1(速通版)(2)

简介: python面向对象操作1(速通版)(1)

3.多行文件读取

读取多行文件

file = open('a.txt','rt')
content = 1
while content:
    content = file.readline()
    print(content,end="")
file.close()

 

文件读取的多个方式

前面演示了三个,我们直接演示readlines

file = open('a.txt','rt')
content = file.readlines()
print(content)
print("-----------------------------------------------------------------------")
for i in content:
    print(i,end="")
file.close()

 

六、常用文件和文件夹操作

1.文件重命名

#重命名
import os
file = open('a.txt','w')
file.close()
f = os.rename("a.txt",'b.txt')
print(f)

 

2.删除文件

import os
os.remove("b.txt")

3.创建文件夹

import os
os.mkdir('dir')

4.获取当前目录

import os
f = os.getcwd()
print(f)

5.改变默认目录

import os
f = os.chdir('C:/Users/o/PycharmProjects/pythonProject2/dir/')
print(f)
f = os.getcwd()
print(f)

6.获取目录列表

import os
f = os.listdir("./")
print(f)
print(type(f))

7.删除文件夹

注意权限问题

import os
f = os.rmdir("dir")
print(f)

8.文本文件练习

def file_copy(src,dst):
    file_r = open(src,'r')
    file_w = open(dst,'w')
    while True:
        content = file_r.read(1024)
        if content == '':
            print('文件拷贝完成')
            break
        file_w.write(content)
    file_r.close()
    file_w.close()
file_copy('b.txt','a.txt')

import sys
def file_copy(src,dst):
    file_r = open(src,'r')
    file_w = open(dst,'w')
    while True:
        content = file_r.read(1024)
        if content == '':
            print('文件拷贝完成')
            break
        file_w.write(content)
    file_r.close()
    file_w.close()
src = sys.argv[1]
dst = sys.argv[2]
file_copy(src,dst)

上面的代码只能copy文本文件

9.二进制文件练习

import sys
def file_copy(src,dst):
    file_r = open(src,'rb')
    file_w = open(dst,'wb')
    while True:
        content = file_r.read(1024)
        #结束
        if content == b'':
            print('文件拷贝完成')
            break
        file_w.write(content)
    file_r.close()
    file_w.close()
src = sys.argv[1]
dst = sys.argv[2]
file_copy(src,dst)

 

七、练习

批量修改文件名

import os
def file_rename(src,dst):
    #根据src得到目录所有文件
    files = os.listdir(src)
    print(files)
    #创建一个目录存放修改名字后的文件
    os.mkdir(dst)
    #修改名字
    for file in files:
        s_file = file.partition('.')
        print(s_file)
        dst_file = s_file[0] + s_file[1] + s_file[2]
        print(dst_file)
        f = open(src+'/'+file,'rb')
        w = open(dst+'/'+dst_file,'wb')
        while True:
            content = f.read(1024)
            if content == b'':
                print("copy完毕")
                break
            w.write(content)
src = 'C:/Users/o/PycharmProjects/pythonProject2/dir1'
dst = 'C:/Users/o/PycharmProjects/pythonProject2/dir'
file_rename(src,dst)

八、类

1.定义一个类

class 类名

       方法列表

定义一个英雄类

#class Haro:
#class Hero()
class Hero():
    def info(self):
        print("i am hero!")

在类中定义的函数叫做方法

在类外定义的函数都叫函数

相关文章
|
1天前
|
Python
【Python操作基础】——帮助文档
【Python操作基础】——帮助文档
|
1天前
|
Python
【Python操作基础】——包
【Python操作基础】——包
|
1天前
|
Python
【Python操作基础】——函数
【Python操作基础】——函数
|
1天前
|
Python
【Python操作基础】——字典,迭代器和生成器
【Python操作基础】——字典,迭代器和生成器
|
1天前
|
Python
【Python操作基础】——集合
【Python操作基础】——集合
|
1天前
|
索引 Python
【Python操作基础】——序列
【Python操作基础】——序列
|
1天前
|
Python
【Python操作基础】——字符串
【Python操作基础】——字符串
|
1天前
|
Python
【Python操作基础】——元组
【Python操作基础】——元组
|
1天前
|
Python
【Python操作基础】——列表操作
【Python操作基础】——列表操作
|
1天前
|
Python
【Python操作基础】——while语句用法和pass语句
【Python操作基础】——while语句用法和pass语句