文件是计算机使用的重要组成部分。文件用于存储不同类型的数据,如文本、图像、表格数据、语音等。每个文件都有一个名称,以区别于其他文件,并能够选择不同的操作。我们可以在文件创建过程中设置文件名,但以后也可以更改。Python编程语言提供 os.rename() 和 shutils.move() 函数以更改文件名。我们还可以使用这些函数来添加时间戳或更改特定的文件扩展名/类型名称。这些函数可以用于Windows和Linux。
使用os.Rename()函数重命名文件
这个 重命名() 通过 操作系统模块 是重命名文件的标准函数。由于系统相关的功能,操作系统模块内部提供了rename()函数。让我们看看 os.rename() 函数语法如下。
os.rename(src,dst,src_dir,dst_dir)
- src公司 是我们要更改或重命名的源文件名。如果是当前工作目录或指定了srcu diru fd,则只能是文件名。另外,我们可以提供源文件名的完整路径,而不使用srcu dir。src参数是必须提供的。
- 夏令时 是要更改或重命名的目标文件名。如果文件在当前工作目录中或指定了dstu diru fd,则只能是文件名。此外,我们还可以提供源文件名的完整路径,而无需使用dstu diru fd。必须提供dst参数。
- 源目录 是可选的,可用于指定源文件的路径。
- dst目录fd 是可选的,可用于指定源文件的路径。
现在我们可以做一个简单的例子,将名为poftut.txt的文件重命名为wisetut.txt。请记住,我们应该导入操作系统模块。
import os#Rename poftut.txt into wisetut.txtos.rename('poftut.txt','wisetut.txt')
如果我们想在不同的工作目录下工作,那么我们应该提供文件的完整路径。您可以看到,我们为名为 /家乡/伊斯梅尔 和 /家庭/艾哈迈特 .
import os
#Rename poftut.txt into wisetut.txt
os.rename('/home/ismail/poftut.txt','/home/ahmet/wisetut.txt')
通过连接路径和文件来提供完整路径的另一种更优雅的方法是使用 os.path.join() 功能如下。
import os
source_file = os.path.join('/home/ismail/','poftut.txt')destination_file = os.path.join('/home/ahmetl/','wisetut.txt')#Rename poftut.txt into wisetut.txt
os.rename( source_file , destination_file)
我们可以通过使用 path.exists() 方法,该方法通过os.path模块提供。
import os
import os.path
#Check if poftut.txt existif path.exists("poftut.txt"): #Rename poftut.txt into wisetut.txt
os.rename('poftut.txt','wisetut.txt')else: print("The poftut.txt doesn't exist")
通过使用path.exists()函数检查源文件和目标文件名,可以使文件重命名代码更加完整。您还可以提供文件的完整路径,以检查其是否存在。
import os
import os.path
#Check if poftut.txt exist
if path.exists("poftut.txt") and path.exists("wisetut.txt"):
#Rename poftut.txt into wisetut.txt
os.rename('poftut.txt','wisetut.txt')
else:
print("The poftut.txt doesn't exist or wisetut.txt allready exist")
重命名特定的文件扩展名/类型
另一个流行的用例是根据文件类型的扩展名重命名文件。有不同的方法来实现这一点。但下面我们将提供一些实用的方法。我们将使用 全局函数 的glob库,它可以根据扩展名列出文件并与 for循环 .
import globimport oscount=0for src in glob.glob('*.txt'): dst="WiseTut"+str(count)+".txt"
os.rename(src,dst) count++
重命名多个文件
通过使用循环和枚举文件,可以重命名多个文件。在下面的示例中,我们将使用 os.listdir() 函数获取文件列表并使用for循环进行枚举。在每个步骤中,我们将对当前文件使用os.rename()函数。
import os for count, filename in enumerate(os.listdir("/home/ismail/")): dst="WiseTut"+str(count)+".txt" src = filename os.rename(src,dst)
重命名文件并替换(如果已存在)
如果目标文件已经存在,Python rename()函数将引发错误。这可以通过替换 os.replace() 可以使用函数。replace()函数的语法与rename()函数相同。下面我们将用wisetut.txt替换poftut.txt,即使wisetut.txt已经存在。
import osos.replace('poftut.txt','wisetut.txt')
通过使用shutil.move()函数移动来重命名文件
蟒蛇 舒蒂尔 模块提供 移动() 方法,其中给定的源文件将被移动到指定的目标文件名。移动与重命名操作相同。我们可以使用 shutil.move() 方法以重命名如下所示的文件。下面的代码将在python3.0或更高版本中工作。
import shutilshutil.move("poftut.txt","wisetut.txt")
重命名文件错误
使用os.rename()函数重命名文件不是没有错误的。对于不同的情况,您可能会得到不同的错误。下面我们将列出使用Python编程语言重命名文件时的一些常见错误类型和解决方案。下面的代码将处理异常,以使rename函数完美无瑕。
import ostry: os.rename("poftut.txt","wisetut.txt")except IsADirectoryError: print("The source file name is a directory not a file")except NotADirectoryError: print("Source file name is a directory not a file")except PermissionError: print("There is not permission for the source or destination file")except OSError as er: print(er)
- A董事 如果源是一个文件,而目标是一个存在的目录,则抛出。检查是否没有使用指定的目标文件名命名的目录。
- NotADirectoryError错误 如果源不是文件而是目录,则引发。检查给定的源文件名是否正确。
- 权限错误 主要与当前用户没有读写源文件或目标文件的权限有关。在Linux中使用chmod或在Windows中使用文件/文件夹属性授予当前用户权限。
- O错误 是一个更一般的错误,它可以由于不同的原因抛出。检查文件系统、磁盘或源文件是否已被其他进程打开。