Python | 重命名现有文件(os.rename()方法的示例)
重命名现有文件 (Renaming an existing file)
To change the name of an existing file – we use "rename()" method of "os" module – so to access the "rename()" method, we must have to import the module "os".
要更改现有文件的名称 –我们使用“ os”模块的“ rename()”方法 –因此要访问“ rename()”方法 ,我们必须导入模块“ os” 。
Module import statement: import os
模块导入语句: import os
Syntax of rename() method: os.rename(src, dest)
named ()方法的语法: os.rename(src,dest)
Here, src is the name to the source file (old file) and dest is the name of the destination file (new file name).
在这里, src是源文件(旧文件)的名称, dest是目标文件的名称(新文件的名称)。
Example:
例:
Here is the code to change the name of an existing filename in python... In this example, we are creating a file file1.txt and writing "Hello" in it, then, we are closing the file, renaming file1.txt to myfile.txt. To verify the operations, checking file1.txt exists or not – if file1.txt does not exist, checking myfile.txt if it exists – printing its content and the content will be 'Hello" – which we have written in file1.txt.
这是在python中更改现有文件名的代码...在此示例中,我们创建文件file1.txt并在其中写入“ Hello” ,然后关闭文件,将file1.txt重命名为myfile.txt 。 要验证操作,请检查file1.txt是否存在–如果file1.txt不存在,则检查myfile.txt是否存在–打印其内容,并且内容为“ Hello” –我们已经在file1.txt中编写了该内容。
import os
def main():
# creating a file first
fo = open("file1.txt","wt")
# writing data in it
fo.write("Hello")
# closing the file
fo.close()
# changing the file name
os.rename("file1.txt", "myfile.txt")
# checking that file1.txt exists or not
# if does not exist - will open myfile and read
if not(os.path.exists("file1.txt")):
print("file1.txt does not exist.")
# checking myfile, and read its content
if os.path.exists("myfile.txt"):
print("myfile.txt exists, reading its content...")
# opening file
fo = open("myfile.txt", "rt")
# reading its content
str = fo.read()
# printing the content
print("Content of the file: ")
print(str)
else:
print("Operation failed.")
if __name__=="__main__":main()
Output
输出量
file1.txt does not exist.
myfile.txt exists, reading its content...
Content of the file:
Hello
翻译自: https://www.includehelp.com/python/rename-an-existing-file-example-of-os-rename-method.aspx
Python | 重命名现有文件(os.rename()方法的示例)相关推荐
- 使用python重命名某个文件下的所有的文件
# coding:utf-8 import osallFileNum = 0# 重命名一个目录下的所有文件的名字 def Rename(level, path):global allFileNum # ...
- python重命名csv文件_Python根据文件中选定的字符复制和重命名许多小csv文件
我不是程序员:我是一个飞行员,在过去的生活中只做了一点脚本编写,所以我对这一点完全不了解.我搜索过论坛,发现了一些类似的问题,如果有更多的专业知识和时间,我也许能够适应我的问题,但我希望我可以通过问自 ...
- Hadoop hdfs文件块信息获取/文件重命名/修改文件的副本数量代码示例
- python重命名文件或目录_Python重命名多个文件的实例方法
Python中如何重命名多个文件 在Python3中可以使用rename()方法+listdir()方法来重命名多个文件,这两个方法都是os模块的一部分,非常方便.下面本篇文章就来带大家了解一下ren ...
- python 重命名的方法,python如何重命名文件
欢迎访问少儿编程网(http://www.pxcodes.com)python重命名文件的方法:首先打开pycharm,找到要重命名的文件:然后依次选择[Refactor-Rename],在重命名窗口 ...
- python os rename用法_Python os.rename() 方法
Python os.rename() 方法 概述 os.rename() 方法用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError. 语法 rename()方 ...
- python批量处理视频教程_《自拍教程72》Python批量重命名视频文件,AV专家必备!...
案例故事: 任何一款终端产品只要涉及视频播放,就肯定涉及视频的解码播放测试, 作为一名专业的多媒体测试人员,我们需要一堆的规范的标准视频测试文件, 但是发现现有的视频资源名字命名的很随意比如:big_ ...
- python教学视频h_《自拍教程72》Python批量重命名视频文件,AV专家必备!
案例故事: 任何一款终端产品只要涉及视频播放,就肯定涉及视频的解码播放测试, 作为一名专业的多媒体测试人员,我们需要一堆的规范的标准视频测试文件, 但是发现现有的视频资源名字命名的很随意比如:big_ ...
- python重命名文件源码
把写内容过程中比较重要的内容做个收藏,下面的内容段是关于python重命名文件的内容. import os cur_path = os.getcwd() lists = os.listdir( cur ...
最新文章
- 如何写一个脚本语言_如何写一个Nx schematic plugin?
- linux 64位 shellcode,Linux Shellcode“你好,世界!”
- C#几种常用的排序算法
- 汇编: mul乘法指令(字乘法结果在dx:ax中,8位乘法:一个乘数默认放在al中)
- malloc free
- 微型计算机电源的选购应注意哪些问题,购买小功率ups电源的注意事项
- java if在内存中_java如何将对象暂存到内存中?
- mysql 存储过程改用户_Mysql修改存储过程相关权限问题
- Odoo 14 版本优化更新的新功能体验
- 后台拿webshell的方法总结
- rabbitmq中的队列transient和durable的区别
- 10、32位 x86处理器编程架构
- 后端ajaxPost请求传给前端的显示乱码问题
- (转)来自瑞士的七个葫芦娃:各怀绝技的Wealthtech解决方案商
- C#中IntPtr类型
- Homekit直连的灯带控制套装
- PS教程:如何设置水彩效果?
- 使用Excel数据分析工具进行多元回归分析的方法
- 有机咖啡最佳抗氧化的好处
- 滑动验证码破解—python—以某东网站为例