python - 将整个文件大写
问题描述
我正在将文件附加到我的代码中,然后将文件转换为大写。Rn 它只是将文件大写的第一句变成了大写字母,其余的小写字母我尝试了 2 种不同的方式,我把两者都放了以防万一你想看到视觉效果(一个策略在标签中,而一个在代码中)。我怎样才能使整个文件变成大写?
newFile = open ('tobe.txt', 'r')
new_file = open ('tobeUPPER.txt','w')
#for line in newFile:
#print (newFile.read()),
#wholeFile = newFile.read()
#upperLine = wholeFile.upper()
#print (upperLine)
for line in newFile:
print (newFile.read()),
newFile = open ('tobe.txt', 'r')
wholeFile = line.upper()
print (wholeFile)
new_file.write('tobeUPPER.txt', "w")
newFile.close()
new_file.close()
解决方案
使用 打开这两个文件with
,这将为您关闭文件。读取原始文件的内容,将它们转换为大写,然后将它们写入新文件。
with open('tobe.txt', 'r') as original_file:
with open('tobeUPPER.txt', 'w') as new_file:
new_file.write(original_file.read().upper())
推荐阅读
- html - 如何使用 dbmail (SQLServer) 存储过程将图像嵌入到 Gamil 的 HTML 邮件中
- c++ - 在 C/C++ 和 Ubuntu 中自动选择正确的端口 ttyS*
- linux - 无法在 ZSH 中运行 bash 文件
- python - 使用 for 循环更改多列的类别类型
- vba - 如何创建标签文本框,如 html 标签(在 ms 访问表单中)
- windows - Makefile 错误,make *** 没有指定目标,也没有找到 makefile。停止
- python - 将字符串形式的单词列表转换为列表
- docker - 在 ubuntu 16.04 虚拟框中丢失了我的 docker 网络问题安装
- docker - containerd-shim 如何创建无守护容器?
- laravel - 执行 HTTP 请求我想将延迟时间从 Laravel 添加到外部 API