python - 如何读取 .py 文件并向其中添加代码?
问题描述
我正在尝试.py
读取python 3.6
. 目的是在不手动浏览所有文件和编辑代码的情况下替换/添加一些代码。
import os
py_files = []
for path, subdirs, files in os.walk(location):
for name in files:
if '.py' in name:
py_files.append(os.path.join(path, name))
test_file = py_file[0]
t = open(test_file,'r').read() # Trying a read only to check whether I can see the text
print(t)
# assuming this starts working.
#I want to see the file and then use a .replace and save the file.
#I assume saving it back as `.py` might present a challenge
我得到的输出是''
.
不知道如何完成这项工作。
解决方案
推荐阅读
- react-native - NativeBase - 按钮变暗 onPress
- java - 如何检查一个字符串是否可以由Java中另一个字符串的字符组成?
- java - 如何在 Java 中实现并行二进制排序
- python - 在列表中找到 4 个靠近的值
- c# - 从 C# 将 Sql 设置为单用户模式并创建管理员用户
- css - 当 line-height 什么都不做时,如何降低行高?
- javascript - 对相同值的项目数组进行分组
- python - django runserver 可以工作,但是使用 daphne 会出现错误
- excel - 当我的 vba 代码在一个 chrome 窗口上工作时如何导航到其他 chrome 窗口
- google-app-engine - 我可以在 Google App Engine 中设置最大空闲实例吗?