简体   繁体   English

在多个文件上运行python脚本

[英]Running python script on multiple files

I have a python script that reads a file and extracts certain data from it, then stores the data in a dictionary, and finally inserts that into a mysql table. 我有一个python脚本,该脚本读取文件并从中提取某些数据,然后将数据存储在字典中,最后将其插入到mysql表中。 The first line of my code is where I enter in my filename: 代码的第一行是我输入文件名的位置:

filename = "examplelogfile"

which is a text file, and is later referred to as 'filename'. 这是一个文本文件,以后称为“文件名”。 But I have to run this code on almost a thousand different files of this type, and I've moved all of these files to a certain server (I'm not running the script in terminal since I need to run it on the same server where the mysql database is located). 但是我必须在几乎一千个这种类型的文件上运行此代码,并且我已经将所有这些文件移动到了特定的服务器上(由于我需要在同一服务器上运行该脚本,所以我不在终端机上运行该脚本mysql数据库所在的位置)。 What's the easiest way to run my code on all of the files? 在所有文件上运行代码的最简单方法是什么?

Edit: I have tried this but it isn't giving me any output (it is supposed to print all the dictionaries): 编辑:我已经尝试过,但是它没有给我任何输出(应该打印所有字典):

import glob
files = glob.glob('~/Desktop/pythoncode/*logfile')
for filename in files:
    rest of code
    print dict

Try the glob module to get all files like this: 尝试使用glob模块获取所有如下文件:

import glob
path = 'C:/Users/telli/Desktop/Test Shapes/Shapes/Squares'
filenames = glob.glob(path + '/*.gif')
for filename in filenames:
    # Do something

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM