Python 3 教程 在线

1153Python3 File

看上面分享的笔记,有个大佬打开文件然后没有关闭。。。

一般来说推荐以下方法:

#写
with open('test.txt', 'w', encoding='utf-8') as f:
    f.write('test')
#读
with open('test.txt', 'r', encoding='utf-8') as f:
    f.readlines()

执行完自动close,避免忘记关闭文件导致资源的占用。

1152Python3 File

在上面的例子中,write(),read() 方法默认是写入到当前 .py 同文件夹下面的,此外 w+ 的使用方法:不能直接 write() 后,在进行读取,这样试读不到数据的,因为数据对象到达的地方为文件最后,读取是向后读的,因此,会读到空白,应该先把文件对象移到文件首位

f = open("forwrite.txt", "w+",encoding='utf-8')
f.write("可以 ,你做的很好! 6666")  # 此时文件对象在最后一行,如果读取,将读不到数据
s=f.tell()     # 返回文件对象当前位置
f.seek(0,0)    # 移动文件对象至第一个字符
str=f.read()
print(s,str,len(str))

1151Python3 File

用户输入"xxx.txt"类文档文件名

用户输入被替换的"待替换字"

用户输入替换目标"新的字"

用户判断是否全部替换 yes/no

def file_replace(file_name, rep_word, new_word):
    f_read = open(file_name)

    content = []
    count = 0

    for eachline in f_read:
        if rep_word in eachline:
            count = count+eachline.count(rep_word)
            eachline = eachline.replace(rep_word, new_word)
        content.append(eachline)    

    decide = input('\n文件 %s 中共有%s个【%s】\n您确定要把所有的【%s】替换为【%s】吗?\n【YES/NO】:' \
                   % (file_name, count, rep_word, rep_word, new_word))

    if decide in ['YES', 'Yes', 'yes']:
        f_write = open(file_name, 'w')
        f_write.writelines(content)
        f_write.close()

    f_read.close()


file_name = input('请输入文件名:')
rep_word = input('请输入需要替换的单词或字符:')
new_word = input('请输入新的单词或字符:')
file_replace(file_name, rep_word, new_word)

1150Python3 File

获取文件后缀:

def getfile_fix(filename):
     return filename[filename.rfind('.')+1:]
print(getfile_fix('facesho.txt'))

1149Python3 File

检索指定路径下后缀是 py 的所有文件:

#!/usr/bin/python3

import os
import os.path

#path = 'D:/UC/'
ls = []

def getAppointFile(path,ls):
    fileList = os.listdir(path)
    try:
        for tmp in fileList:
            pathTmp = os.path.join(path,tmp)
            if True==os.path.isdir(pathTmp):
                getAppointFile(pathTmp,ls)
            elif pathTmp[pathTmp.rfind('.')+1:].upper()=='PY':
                ls.append(pathTmp)
    except PermissionError:
        pass

def main():

    while True:
        path = input('请输入路径:').strip()
        if os.path.isdir(path) == True:
            break

    getAppointFile(path,ls)
    #print(len(ls))
    print(ls)
    print(len(ls))

main()