惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Check Point Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
GbyAI
GbyAI
WordPress大学
WordPress大学
月光博客
月光博客
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
博客园 - 司徒正美
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
Blog — PlanetScale
Blog — PlanetScale
B
Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
L
LangChain Blog
腾讯CDC
T
The Blog of Author Tim Ferriss
量子位

博客园 - soulsjie

一种在winfrom窗体中显示计算公式的解决方案 winform窗体DataGridView合并单元格处理 C#GDI+阴影笔刷样式HatchStyle探讨 C#代码混淆工具ConfuserEx的使用 Aspose.Words在指定位置插入图片、调整图片大小 C#获取对象实体的键值对信息 C#将Winform上的TextBox和ComBox的值导入和导出 C#使用Aspose.Words将Spread表格插入到Word中 C# Aspose.Words将word中自定义的标签进行替换 C# 将项目资源文件保存到磁盘上 SqlSugar数据库辅助类 使用存储过程备份MS SQLServer数据库 案例3:JAVA GUI 随机点名程序 案例2:JAVA GUI 简易计算器 ArcGIS JS API 添加要素图层 点击时获取图层属性 ArcGIS JS API 将天地图设置为底图 HTML5PLUS实现类似右侧弹出菜单 SqlSugar各数据库连接串 案例1:JAVAGUI用户管理
Python文件操作基础方法
soulsjie · 2023-01-18 · via 博客园 - soulsjie
import os
import shutil

#创建文件
def CreateFile(filename):
    f=open(filename,mode='a',encoding='utf-8')
    f.close()
    print("-------文件创建成功--------")

#写入文件内容
def AppendFileContent(filename,content):
    f = open(filename, mode='a', encoding='utf-8')
    f.write(content)
    f.close()
    print("-------写入文件内容成功--------")

#读取文件内容
def ReadFile(filename):
    f = open(filename, mode='r', encoding='utf-8')
    for line in f.readlines():
        print(line)
    f.close()

#删除文件
def DelFile(filename):
    print("-------删除文件--------")
    os.remove(filename)
    print("-------删除文件成功--------")

#复制文件
def Copyile(filename,newFilename):
    shutil.copy(filename, newFilename)
    print("-------复制文件成功--------")

#文件重命名
def ReNameFile(filename,newFilename):
    os.rename(filename, newFilename)
    print("-------文件重命名成功--------")

#创建文件
CreateFile("ces.txt")
#往文件中追加内容
AppendFileContent("ces.txt","测试文本内容张三\n")
#读取文件
ReadFile("ces.txt")
#复制文件
Copyile("ces.txt","ces2.txt")
#文件重命名
ReNameFile("ces.txt","ces3.txt")
#最后删除文件
DelFile("ces2.txt")
DelFile("ces3.txt")