最近项目需要将大量的压缩文件导入到数据库中,所以开始总结用Python批量处理的办法,本次是首先将这些压缩文件的文件名提取出来,然后导入到数据库中。

由于涉及到路径的读取处理,所以方法有os模块和commands模块,本次主要采用commands模块(有时间的话,一定要再探索一下os模块实现的方式)。

# encoding: utf-8
#!/usr/bin/python

import commands
import MySQLdb

conn = MySQLdb.connect(
host='localhost',
user='***',
passwd='***',
db='***',
charset='utf8')

cur = conn.cursor()

#path = '/home/***/***/'
res = commands.getstatusoutput( 'find /home/***/***/企业名录/' ) #res 为返回的元组,包含(status,pathlist)即状态码,路径两个元素;find为Linux下查找文件的命令
#res = commands.getstatusoutput( 'find /home/***/***/' )

pathlist = res[1].split('\n') #通过下标为1,提取出元祖res中的路径,并通过字符串方法split,去掉‘\n’,转换成一个一维的路径列表

#for line in pathlist:
# type(line)

for i in range(0,len(pathlist)): #通过for循环将列表中的文件路径取出

fileLine = pathlist[i] #取出后的文件路径为一个字符串

# print type(fileLine)
seperator = fileLine.rfind('/') #通过字符串方法rfind,找到字符‘/’在路径中最后出现的位置,在此位置之前为路径,在此之后为文件名(但是有一个小问题是文件夹的名字是一个路径)
filePath = fileLine[0:seperator+1] #通过切片的方式提取出文件路径
fileName = fileLine[seperator+1:] #通过切片的方式提取出文件名
fileType = fileLine[-3:]

# sql = "insert into table rarFileList(ID,filePath,fileName,fileType) values (%s,%s,%s,%s)"(str(i),filePath,fileName,fileType)
# sql = "insert into table rarFileList(ID,filePath,fileName,fileType) values (" + str(i) + "," + filePath + "," + fileName + "," + fileType + ")"  #此处为多种方式尝试,需要小心会有一些数据类型错误等情况出现
# print sql
# cur.execute(sql)
cur.execute("insert into rarFileList(ID,filePath,fileName,fileType) values (%s,%s,%s,%s)",(i,filePath,fileName,fileType))
# if fileLine[-3:] == 'rar':
# sql = "update rarFileList set status= 'done' where fileType = 'rar'"
# cur.execute(sql)
# elif fileLine[-3:]== 'zip':
# sql = "update rarFileList set status= 'done' where fileType ='zip'"
# cur.execute(sql)
cur.close()
conn.commit()
conn.close()

Python调用MYSQL,将文件名和路径批量入库用法小结的更多相关文章

  1. python3.4学习笔记(二十五) Python 调用mysql redis实例代码

    python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...

  2. python调用py中rar的路径问题。

    1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...

  3. python 调用mysql存储过程返回结果集

    存储过程: delimiter | ),)) begin select * from tb_test where mid = imid and user = iuser; end; | delimit ...

  4. python 读取mysql存储的文件路径下载文件,内容解析,上传七牛云,内容入es

    #!/usr/bin/env python # -*- coding: utf-8 -*- import ConfigParser import json import os import re fr ...

  5. 一个小玩具:Python调用Mysql

    1. ubuntu安装MySQL how to install:$ sudo apt-get install mysql-server$ sudo apt-get install mysql-clie ...

  6. python调用MySQL数据库

    在Python中访问mysql数据库中的数据需要三步骤: 1,建立连接 2,操作数据库 3,连接关闭

  7. Python调用MySQL的一些用法小结

    目标:1个excel表内容导入到数据库中,例如:原始excel文件为 aaa.xls 首先:将aaa.xls 转换成aaa.txt ,注意当文件中含有中文字符时,可以通过notepad++打开,在“格 ...

  8. Python 执行 mysql 存储过程

    mysql 创建简单的临时表 tmp create database test; use test; DROP TABLE IF EXISTS `tmp`; CREATE TABLE `tmp` ( ...

  9. 使用python读取mysql数据库并进行数据的操作

    (一)环境的配置 使用python调用mysql数据库要引进一些库. 目前我使用的python版本是python3.6.引进库为pymysql 其他对应的库可以有以下选择: mysqldb,oursq ...

随机推荐

  1. 21.Shiro在springboot与vue前后端分离项目里的session管理

    1.前言 当决定前端与后端代码分开部署时,发现shiro自带的session不起作用了. 然后通过对请求head的分析,然后在网上查找一部分解决方案. 最终就是,登录成功之后,前端接收到后端传回来的s ...

  2. oracle聚簇表的理解 (转自:https://blog.csdn.net/gumengkai/article/details/51009345 )

    Oracle支持两种类型的聚簇:索引聚簇和哈希聚簇 一.索引聚簇表的原理 聚簇:如果一些表有一些共同的列,则将这样一组表存储在相同的数据块中 聚簇还表示把相关的数据存储在同一个块上.利用聚簇,一个块可 ...

  3. python之ProcessPoolExecutor

    ProcessPoolExecutor使用上基本与ThreadPoolExecutor一致不过在windows上使用,有个问题需要注意.使用不当会出现如下错误 File "...\lib\m ...

  4. iOS之点击通知栏跳转应用的相关页面

    当远程推送通知到达应用,有3个相关的方法是用来处理这个通知的. - (BOOL)application:(UIApplication *)application didFinishLaunchingW ...

  5. 【2017 World Final E】Need For Speed(二分)

    Sheila is a student and she drives a typical student car: it is old, slow, rusty, and falling apart. ...

  6. 【2018 ICPC南京网络赛 A】An Olympian Math Problem(数论题)

    Alice, a student of grade 6, is thinking about an Olympian Math problem, but she feels so despair th ...

  7. Flask中对MySQL的基本操作

    在Flask-SQLAlchemy中,插入.修改.删除操作,均由数据库会话管理. 会话用 db.session 表示.在准备把数据写入数据库前,要先将数据添加到会话中然后调用 commit() 方法提 ...

  8. WebSocket协议 8 问

    WebSocket是一种比较新的协议,它是伴随着html5规范而生的,虽然还比较年轻,但大多主流浏览器都已经支持.它使用方面.应用广泛,已经渗透到前后端开发的各种场景中. 对http一问一答中二式流程 ...

  9. ERROR: bootstrap checks failed

    错误描述:Linux默认配置的参数过小,需要自己设置 max file descriptors [4096] for elasticsearch process is too low, increas ...

  10. 06 python操作MySQL和redis(进阶)

    python操作mysql.redis 阶段一.mysql事务 主要用于处理操作量大,复杂度高的数据.比如说,在人员管理系统中,你删除一个人员,你即需要删除人员的基本资料,也要删除和该人员相关的信息, ...