Python calls MySQL to summarize file names and paths in bulk storage usage

Source: Internet
Author: User
Tags rar

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

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.