Python调用MYSQL,将文件名和路径批量入库用法小结
最近项目需要将大量的压缩文件导入到数据库中,所以开始总结用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,将文件名和路径批量入库用法小结的更多相关文章
- python3.4学习笔记(二十五) Python 调用mysql redis实例代码
python3.4学习笔记(二十五) Python 调用mysql redis实例代码 #coding: utf-8 __author__ = 'zdz8207' #python2.7 import ...
- python调用py中rar的路径问题。
1.python调用py,在py中的os.getcwd()获取的不是py的路径,可以通过os.path.split(os.path.realpath(__file__))[0]来获取py的路径. 2. ...
- python 调用mysql存储过程返回结果集
存储过程: delimiter | ),)) begin select * from tb_test where mid = imid and user = iuser; end; | delimit ...
- python 读取mysql存储的文件路径下载文件,内容解析,上传七牛云,内容入es
#!/usr/bin/env python # -*- coding: utf-8 -*- import ConfigParser import json import os import re fr ...
- 一个小玩具:Python调用Mysql
1. ubuntu安装MySQL how to install:$ sudo apt-get install mysql-server$ sudo apt-get install mysql-clie ...
- python调用MySQL数据库
在Python中访问mysql数据库中的数据需要三步骤: 1,建立连接 2,操作数据库 3,连接关闭
- Python调用MySQL的一些用法小结
目标:1个excel表内容导入到数据库中,例如:原始excel文件为 aaa.xls 首先:将aaa.xls 转换成aaa.txt ,注意当文件中含有中文字符时,可以通过notepad++打开,在“格 ...
- Python 执行 mysql 存储过程
mysql 创建简单的临时表 tmp create database test; use test; DROP TABLE IF EXISTS `tmp`; CREATE TABLE `tmp` ( ...
- 使用python读取mysql数据库并进行数据的操作
(一)环境的配置 使用python调用mysql数据库要引进一些库. 目前我使用的python版本是python3.6.引进库为pymysql 其他对应的库可以有以下选择: mysqldb,oursq ...
随机推荐
- JS apply 和 call 的实现
很早之前的一篇博客写了bind的实现,是基于apply的,感兴趣的朋友看完这篇文章可以接着看看bind的实现. apply 和 call 主要就是传参的区别.这里就不多说了,直接看代码. //call ...
- C.Sum 2017 ACM-ICPC 亚洲区(西安赛区)网络赛
题目来源:Sum 限制:1000ms 32768K Define the function S(x) for xx is a positive integer. S(x) equals to the ...
- ABAP术语-Object Type
Object Type 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/06/1093159.html Description created ...
- Co. - Microsoft - Windows - Tomcat、JDK、MySQL通过 Inno 集成为exe部署包
需求 客户设备为Windows系统,需要部署公司产品,因此将Tomcat.JDK.MySQL.Java.war 打包整合成exe文件,Windows下一键部署安装. 最佳实践 1.下载免安装的mysq ...
- visual studio 2015密钥
Visual Studio Professional 2015简体中文版(专业版)KEY:HMGNV-WCYXV-X7G9W-YCX63-B98R2Visual Studio Enterprise 2 ...
- thinkphp-PHP实现pdf导出功能
Thinkphp框架引用tcpdf插件,插件下载地址:待续... 代码编写前先引入tcpdf整个文件夹到项目目录的ThinkPHP文件夹下 如:/ThinkPHP/Library/Vendor/tcp ...
- phpstorm代码提示不小心关了,如何开启
在phpstrom右下角单击如图 出现event log窗口 如果不是 单击切换取消节电模式即可开启代码提示.
- Python学习:If 语句与 While 语句
If 语句 用以检查条件:如果条件为真(True),将运行这一块的语句(称作 if-block 或 if 块) 则将运行另一块语句(称作 else-block 或 else 块),其中 else ...
- 007---logging日志模块
logging模块 用途:服务器运行日志.运维日志... import logging from logging.handlers import RotatingFileHandler, TimedR ...
- python基础之反射、面向对象进阶
isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象,如果是返回True 1 class F ...