重命名现有文件 (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()方法的示例)相关推荐

  1. 使用python重命名某个文件下的所有的文件

    # coding:utf-8 import osallFileNum = 0# 重命名一个目录下的所有文件的名字 def Rename(level, path):global allFileNum # ...

  2. python重命名csv文件_Python根据文件中选定的字符复制和重命名许多小csv文件

    我不是程序员:我是一个飞行员,在过去的生活中只做了一点脚本编写,所以我对这一点完全不了解.我搜索过论坛,发现了一些类似的问题,如果有更多的专业知识和时间,我也许能够适应我的问题,但我希望我可以通过问自 ...

  3. Hadoop hdfs文件块信息获取/文件重命名/修改文件的副本数量代码示例

  4. python重命名文件或目录_Python重命名多个文件的实例方法

    Python中如何重命名多个文件 在Python3中可以使用rename()方法+listdir()方法来重命名多个文件,这两个方法都是os模块的一部分,非常方便.下面本篇文章就来带大家了解一下ren ...

  5. python 重命名的方法,python如何重命名文件

    欢迎访问少儿编程网(http://www.pxcodes.com)python重命名文件的方法:首先打开pycharm,找到要重命名的文件:然后依次选择[Refactor-Rename],在重命名窗口 ...

  6. python os rename用法_Python os.rename() 方法

    Python os.rename() 方法 概述 os.rename() 方法用于命名文件或目录,从 src 到 dst,如果dst是一个存在的目录, 将抛出OSError. 语法 rename()方 ...

  7. python批量处理视频教程_《自拍教程72》Python批量重命名视频文件,AV专家必备!...

    案例故事: 任何一款终端产品只要涉及视频播放,就肯定涉及视频的解码播放测试, 作为一名专业的多媒体测试人员,我们需要一堆的规范的标准视频测试文件, 但是发现现有的视频资源名字命名的很随意比如:big_ ...

  8. python教学视频h_《自拍教程72》Python批量重命名视频文件,AV专家必备!

    案例故事: 任何一款终端产品只要涉及视频播放,就肯定涉及视频的解码播放测试, 作为一名专业的多媒体测试人员,我们需要一堆的规范的标准视频测试文件, 但是发现现有的视频资源名字命名的很随意比如:big_ ...

  9. python重命名文件源码

    把写内容过程中比较重要的内容做个收藏,下面的内容段是关于python重命名文件的内容. import os cur_path = os.getcwd() lists = os.listdir( cur ...

最新文章

  1. 如何写一个脚本语言_如何写一个Nx schematic plugin?
  2. linux 64位 shellcode,Linux Shellcode“你好,世界!”
  3. C#几种常用的排序算法
  4. 汇编: mul乘法指令(字乘法结果在dx:ax中,8位乘法:一个乘数默认放在al中)
  5. malloc free
  6. 微型计算机电源的选购应注意哪些问题,购买小功率ups电源的注意事项
  7. java if在内存中_java如何将对象暂存到内存中?
  8. mysql 存储过程改用户_Mysql修改存储过程相关权限问题
  9. Odoo 14 版本优化更新的新功能体验
  10. 后台拿webshell的方法总结
  11. rabbitmq中的队列transient和durable的区别
  12. 10、32位 x86处理器编程架构
  13. 后端ajaxPost请求传给前端的显示乱码问题
  14. (转)来自瑞士的七个葫芦娃:各怀绝技的Wealthtech解决方案商
  15. C#中IntPtr类型
  16. Homekit直连的灯带控制套装
  17. PS教程:如何设置水彩效果?
  18. 使用Excel数据分析工具进行多元回归分析的方法
  19. 有机咖啡最佳抗氧化的好处
  20. 滑动验证码破解—python—以某东网站为例

热门文章

  1. python matplotlib 绘制曲线图,柱状图
  2. Android手机用wifi连接adb调试的方法
  3. access注入漏洞
  4. linux软件安装管理
  5. Android 带着用户名的SharedPreferences
  6. java web判断服务器是否是本机
  7. No style sheet with given id found错误
  8. MFC中的几个常用类——CFileDialog
  9. Chromium OS 开源项目
  10. SQL – 2.SQLServer的管理 + 3.SQL基础1 + 4.SQL基础2