Recent projects need to import a large number of compressed files into the database, so began to summarize the Python batch processing method, this is the first to extract the file names of these compressed files, and then import into the database.
Because of the access to the path of the read processing, so the method has an OS module and commands module, the main use of the commands module (if there is time, we must explore the way the OS module implementation).
# 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/***/***/enterprise directory/') #res for the returned tuple, containing (status,pathlist) a status code, path two elements Find is a command for finding files under Linux
#res = commands.getstatusoutput ( ' Find /home/***/***/')
PathList = Res[1].split (' \ n ') #通过下标为1, extract the path from the progenitor res and split it by a string method, removing ' \ n ' and converting it into a one-dimensional path list
#for Line in PathList:
#type (line)
For I in range (0,len (pathlist)): #通过for循环将列表中的文件路径取出
Fileline = Pathlist[i] #取出后的文件路径为一个字符串
#print type (fileline)
Seperator = Fileline.rfind ('/') #通过字符串方法rfind, find the last position in the path where the character '/' appears, before this position is the path, after which is the file name (but there is a small problem that the name of the folder is a path)
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 + ")" #此处为多种方式尝试, need to be careful there will be some data type errors etc.
#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 calls MySQL to summarize file names and paths in bulk storage usage