文件交互:输入-->运算-->输出 

养成随时保存文件的习惯Ctrl+S:.exe/.txt/.ppt/.jpg/.mp4/.avi

 打开模式:

文件对象方法

f = open('D:\\record.txt', 'rt')
f = open('D:\\record.txt', 'r')
f = open('D:\\record.txt')
f.read()
f.read()
f.close()
f = open('D:\\record.txt')
f.read(10)
f.tell()
f.seek(45, 0)
f.readline()
list(f)
f.seek(0, 0)

lines = list(f)                  #不写此句调用效率更高
for each_line in lines:
print(each_line)

f.seek(0, 0)

f = open('D:/test.txt', 'w')
f.write('我爱鱼C工作室')
f.close()                                #产生一个新文档

 文本编辑

f = open('record.txt')

boy = []
girl = []
count = 1

for each_line in f:
    if each_line[:6] != '======':
        (role, line_spoken) = each_line.split(':', 1)    #进行字符串的分割操作
        if role == '小甲鱼':
            boy.append(line_spoken)
        if role == '小客服':
            girl.append(line_spoken)
    else:
        file_name_boy = 'boy_' + str(count) + '.txt'         #进行文本的保存操作
        file_name_girl = 'girl_' + str(count) + '.txt'

        boy_file = open(file_name_boy, 'w')
        girl_file = open(file_name_girl, 'w')

        boy_file.writelines(boy)
        girl_file.writelines(girl)

        boy_file.close()
        girl_file.close()

        boy = []
        girl = []
        count += 1

file_name_boy = 'boy_' + str(count) + '.txt'
file_name_girl = 'girl_' + str(count) + '.txt'

boy_file = open(file_name_boy, 'w')
girl_file = open(file_name_girl, 'w')

boy_file.writelines(boy)
girl_file.writelines(girl)

boy_file.close()
girl_file.close()

f.close()






def save_file(boy, girl, count):
    file_name_boy = 'boy_' + str(count) + '.txt'
    file_name_girl = 'girl_' + str(count) + '.txt'

    boy_file = open(file_name_boy, 'w')
    girl_file = open(file_name_girl, 'w')

    boy_file.writelines(boy)
    girl_file.writelines(girl)

    boy_file.close()
    girl_file.close()


def split_file(file_name):
    f = open('record.txt')

    boy = []
    girl = []
    count = 1

    for each_line in f:
        if each_line[:6] != '======':
            (role, line_spoken) = each_line.split(':', 1)
            if role == '小甲鱼':
                boy.append(line_spoken)
            if role == '小客服':
                girl.append(line_spoken)
        else:
            save_file(boy, girl, count)

            boy = []
            girl = []
            count += 1

    save_file(boy, girl, count)

    f.close()


split_file('record.txt')

 

模块导入import,包括变量和函数等文件,可被其他程序引入后使用

模块:Mac OS,Windows,Linux,UNIX等,对文件系统访问原理是不一样的

>>> import os
>>> os.getcwd()
'C:\\ProgramData\\Anaconda3\\Scripts'
>>> os.chdir('D:\\')
>>> os.getcwd()
'D:\\'
>>> os.listdir('D:\\')
['$RECYCLE.BIN', '360安全浏览器下载', 'Gaomin', 'Python Project', 'Python视频', 'QQMusicCache', 'record.txt', 'record1.txt', 'Software', 'System Volume Information', 'test1.txt', 'test10.txt', '心理学']
>>> os.mkdir('D:\\A')
>>> os.mkdir('D:\\A\\B')
>>> os.mkdir('D:\\C\\B')
Traceback (most recent call last):
  File "<pyshell#10>", line 1, in <module>
    os.mkdir('D:\\C\\B')
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'D:\\C\\B'
>>> os.makedirs(('D:\\A\\B\\test.txt'))
>>> os.rmdir(('D:\\A\\B\\test.txt'))
>>> os.rmdir(('D:\\A\\B'))
>>> os.rmdir(('D:\\A\\B\\test.txt'))#逐层删除
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    os.rmdir(('D:\\A\\B\\test.txt'))#逐层删除
FileNotFoundError: [WinError 3] 系统找不到指定的路径。: 'D:\\A\\B\\test.txt'
>>> os.rmdirs(('D:\\A))#逐层删除
       
SyntaxError: EOL while scanning string literal
>>> os.system('cmd')

 

>>> import os
>>> os.path.basename('D:\\A\\B\\A.avi')
'A.avi'
>>> os.listdir(os.curdir)
>>> os.path.dirname('D:\\A\\B\\A.avi')
'D:\\A\\B'
>>> os.path.join('D:\\A','A','B','A.avi')
'D:\\A\\A\\B\\A.avi'
>>> os.path.dirname('D:\\A\\B\\A.avi')
'D:\\A\\B'
>>> os.path.join('D:\\A','A','B','A.avi')
'D:\\A\\A\\B\\A.avi'
>>> import time
>>> time.gmtime(os.path.getatime('D:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=4, tm_hour=22, tm_min=26, tm_sec=19, tm_wday=3, tm_yday=4, tm_isdst=0)
>>> time.localtime(os.path.getatime('D:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=5, tm_hour=6, tm_min=26, tm_sec=19, tm_wday=4, tm_yday=5, tm_isdst=0)
>>> time.localtime(os.path.getatime('D:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=5, tm_hour=6, tm_min=26, tm_sec=19, tm_wday=4, tm_yday=5, tm_isdst=0)
>>> time.localtime(os.path.getctime('D:\\A\\test.txt'))
time.struct_time(tm_year=2018, tm_mon=1, tm_mday=5, tm_hour=6, tm_min=26, tm_sec=19, tm_wday=4, tm_yday=5, tm_isdst=0)
>>> A\\
SyntaxError: unexpected character after line continuation character
>>> os.path.ismount('D:\\A'))
SyntaxError: invalid syntax
>>> 
KeyboardInterrupt
>>> os.path.ismount('D:\\')
True
>>> os.path.ismount('D:\\A')
False

 

posted on 2018-01-06 16:54  Samyll  阅读(199)  评论(0编辑  收藏  举报