python批量修改某一文件夹下的所有文件名

 1 import os,shutil
 2
 3 file_path = "E:/downloads/068 分布式爬虫2期/视频"
 4 # 改变工作文件夹
 5 os.chdir(file_path)
 6
 7 # 遍历该文件夹所有的文件,并for循环
 8 for name in os.listdir(file_path):
 9     print(name)
10
11     # 修改文件名
12     new_name = name.split("  ")[1]
13     print(new_name)
14
15     # 文件名加上文件夹构成绝对路径
16     before_file = os.path.join(file_path, name)
17     after_file = os.path.join(file_path, new_name)
18
19     print(‘rename "%s" to "%s"......‘%(before_file,after_file))
20     # 利用shutil.move将文件移动到原来位置(重命名的效果)
21     shutil.move(before_file, after_file)

我这边文件夹下的所有文件都是"某某机构  ***爬虫.flv",所以直接去掉“某某机构”留下正经的文件名

基本上os.listdir()加上shutil.move()搞定

还有更easy的方法,就是os.rename(oldname,newname)。推荐使用这个。

原文地址:https://www.cnblogs.com/baojinjin/p/8831243.html

时间: 2024-08-25 02:28:42

python批量修改某一文件夹下的所有文件名的相关文章

VBS 批量修改多个文件夹下的文字命名

Function FilesTree(sPath)      Set oFso = CreateObject("Scripting.FileSystemObject")      Set oFolder = oFso.GetFolder(sPath)      Set oSubFolders = oFolder.SubFolders        For Each oSubFolder In oSubFolders         path=oSubFolder.Path       

Python批量重命名指定文件夹下文件的两种方法

#法一 import os path = "C://Python34//" for file in os.listdir(path): if os.path.isfile(os.path.join(path,file))==True: if file.find('.')<0: newname=file+'.jpg' os.rename(os.path.join(path,file),os.path.join(path,newname)) #法二 import os import

Python批量按顺序命名文件夹

1 """ Python批量按顺序命名文件夹 """ 2 import os 3 4 path1 = 'E:/侯俊龙数据/1' # 所需修改文件夹所在路径 5 dirs = os.listdir(path1) 6 7 i = 1 8 for dir in dirs: 9 print(dir) 10 os.rename(os.path.join(path1, dir), os.path.join(path1, str(dir[:3])+str(&q

C++ 获取文件夹下的所有文件名

获取文件夹下所有的文件名是常用的功能,今天再一次有这样的需求,所有就在网上查找了很多,并记下以供后用. 原文:http://blog.csdn.NET/cxf7394373/article/details/7195661 原文:http://qiaoxinwang.blog.163.com/blog/static/86096452010612139172/ 头文件:#include<io.h> 1 char * filePath = "D:\\sample"; 1 vect

matlab从文件夹名中获得该文件夹下所图像文件名

function [s,nameC]=get_FileNameFromFolderPath(path) % 函数调用:[s,nameC]=get_FileNameFromFolderPath(path) % 函数功能:从文件夹名中获得该文件夹下所图像文件名 % 函数输入:path 为文件夹路径 % 函数输出:s 表示状态,当为0时表示正确 % nameC 表示获得的图像名的胞体cell % 函数历史:v0.0 @2013-01-27 created by Aborn cmd = sprintf(

python中读取某个路径文件夹下所有文件--listdir()

描述: 当需要读取某个文件下的所有文件时,可以使用listdir()函数,使用该函数之前,需导入模块:from os import listdir 语法: listdir('file path/file name') 参数: file path:文件夹路径,可以是相对路径或绝对路径 file name:文件夹名 返回值: 返回file name下的所有文件名组成的list 实例: 从E:\Github\machine-learning-in-action\chapter 2 knn\digit

Python实现重命名一个文件夹下的图片

在网上查了一下python实现的图片重命名,工作中刚好用一下. 1 # -*- coding:utf8 -*- 2 import os 3 path = '新建文件夹 (2)/' 4 filelist = os.listdir(path) 5 for item in filelist: 6 # print('item name is ',item) 7 if item.endswith('.jpg'): 8 name = item.split('.',1)[0] 9 src = os.path.

Java—— 获取指定文件夹下的所有文件名

递归获取某目录下的所有文件名,路径. 1 package test; 2 3 import java.io.File; 4 5 public class GetFileName{ 6 7 public static void main(String[] args) { 8 // This is the path where the file's name you want to take. 9 String path = "C:\\ProgramData"; 10 getFile(pa

C++获取指定文件夹下的所有文件名(或路径)

#include <io.h> #include <fstream> #include <string> #include <vector> #include <iostream> using namespace std; //其实两个字符串连在一起比如string可惜写成:str1+str2+“xxx”... //获取所有的文件名 void GetAllFiles( string path, vector<string>&