iT邦幫忙

0

請教有關glob.glob python用法

  • 分享至 

  • xImage

請教各位高手
目前想測試將某個路徑底(含子資料匣)下的特定副檔名檔案完整檔案路徑寫到資料庫裏面
目前遇到的問題是子資料匣的相同附檔檔案抓不到
主要搜索資料匣路徑為D:\outlook 我想也能抓到D:\outlook\test底下的PST檔
以下附上我的程式碼

import glob
import pymysql
import os
db_settings = {
    "host": "127.0.0.1",
    "port": 3306,
    "user": "root",
    "password": "p@ssworld",
    "db": "test_01",
    "charset": "utf8"
}
# 建立Connection物件
connect_db = pymysql.connect(**db_settings)

cur = None
if connect_db is not None:
    cur = connect_db.cursor()
if cur is not None:
    sql = 'CREATE TABLE IF NOT EXISTS `file_list`( \
    id INT AUTO_INCREMENT PRIMARY KEY, \
    `file_name` VARCHAR(200) NOT NULL \
    )ENGINE=InnoDB'
    cur.execute(sql)
    
with connect_db.cursor() as cursor:
    dirpathpattern = r"D:\OUTLOOK"
    result = glob.glob(os.path.join(dirpathpattern,"*.pst"))
    for f in result:
        ##pd.read_csv(file_name).iloc[:,1:].to_sql(file_name.replace('.pst',''),db,if_exists='replace')
        sql = "INSERT INTO file_list (file_name) VALUES ('"+f+"')"
        print(f)
        # 執行 SQL 指令
        cursor.execute(sql)
        # 提交至 SQL
        connect_db.commit()
# 關閉 SQL 連線
connect_db.close()import glob
import pymysql
import os
db_settings = {
    "host": "127.0.0.1",
    "port": 3306,
    "user": "root",
    "password": "p@ssworld",
    "db": "test_01",
    "charset": "utf8"
}
# 建立Connection物件
connect_db = pymysql.connect(**db_settings)

cur = None
if connect_db is not None:
    cur = connect_db.cursor()
if cur is not None:
    sql = 'CREATE TABLE IF NOT EXISTS `file_list`( \
    id INT AUTO_INCREMENT PRIMARY KEY, \
    `file_name` VARCHAR(200) NOT NULL \
    )ENGINE=InnoDB'
    cur.execute(sql)
    
with connect_db.cursor() as cursor:
    dirpathpattern = r"D:\OUTLOOK"
    result = glob.glob(os.path.join(dirpathpattern,"*.pst"))
    for f in result:
        ##pd.read_csv(file_name).iloc[:,1:].to_sql(file_name.replace('.pst',''),db,if_exists='replace')
        sql = "INSERT INTO file_list (file_name) VALUES ('"+f+"')"
        print(f)
        # 執行 SQL 指令
        cursor.execute(sql)
        # 提交至 SQL
        connect_db.commit()
# 關閉 SQL 連線
connect_db.close()

產生結果如下
https://ithelp.ithome.com.tw/upload/images/20230925/20128836yFMKwtMrQQ.png

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

我要發表回答

立即登入回答