脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现的读取文件内容并写入其他文件操作示例

Python实现的读取文件内容并写入其他文件操作示例

2021-06-14 00:27xuezhangjun Python

这篇文章主要介绍了Python实现的读取文件内容并写入其他文件操作,结合实例形式分析了Python文件读写操作相关实现技巧,需要的朋友可以参考下

本文实例讲述了python实现的读取文件内容并写入其他文件操作。分享给大家供大家参考,具体如下:

文件目录结构,如图:

read_file.py是工作文件,file_test.py是读取文件源,write_test.py是写入目标文件。

Python实现的读取文件内容并写入其他文件操作示例

文件a:file_test.py

?
1
2
3
#coding=utf-8
for i in range(1, 10):
  print i

文件b:read_file.py

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# coding=utf-8
# 打开件a
f = open('./file_test.py', 'rb')
# 读取文件a内容
print '---------- read file ---------'
content = f.read()
print 'content = ', content
# 打开文件b
w = open('./write_test.py', 'wd')
print '--------- write file --------'
# 将文件a的内容,写入文件b
w.write(content)
print '--------- close file --------'
# 关闭文件b
w.close()
# 关闭文件b
f.close()
print '--------- end ----------'

文件c:write_file.py

?
1
2
3
#coding=utf-8
for i in range(1, 10):
  print i

希望本文所述对大家python程序设计有所帮助。

原文链接:https://blog.csdn.net/xuezhangjun0121/article/details/88584738

延伸 · 阅读

精彩推荐