如何在多个文件上运行脚本python

时间:2019-06-19 03:40:38

标签: python pandas dataframe

我在一个文件夹(file1,file2,file3,file4,file5,....)中有多个.txt文件,我需要在大量文件上运行。

我创建了此代码以重新排序并进行一些更改...我想将其应用于原始文件夹(路径)中的多个txt文件,并将它们分别保存到另一个文件夹(在最后一个文件夹内)或另一个路径

有人可以帮忙吗?

    import pandas as pd
    import numpy as np
    import os
    import glob
    import datetime
    #from datetime import datetime

    df1=pd.read_csv("D:\\Spyder2019\\38A.txt", sep='\s+') 

#############################################33#
#to change name of column and add Temp ms column

    df1=df1.rename(index=str, columns={"Temps":"Heure", "Force":"Force(N)", "Vitesse":"Vitesse(RPM)", "Puissance":"Puissance(w)","Torque":"Torque(N/m)","Angle":"Angle(deg)"})#to change name Temps to Heure

    dtemp=df1['Heure']#to change datetime values into seconds and microseconds in Heure

    dtemp=pd.to_datetime(df1.Heure) #change to datetime values float 

    dtemp1=dtemp.dt.microsecond #dtemp1 object in microseconds with 6 decimals

    dtempms=dtemp1 / 1000000 #dtemp1 object into 2 decimals

    dtemp2=dtemp.dt.second #dtemp2 object in seconds

    df1['Temps_ms']= dtempms + dtemp2 #add second and microseconds to the object

    #################################################
    # to transform the Heure data into sec and microsec

    df1=df1[['Date','NoBille','Heure','Temps_ms','Force(N)','Vitesse(RPM)','Puissance(w)','Torque(N/m)','Angle(deg)']]# reorder the dataframe

    io=df1.iat[0,3]
    #to transform the Heure data into seconds ans microseconds

    df1['Temps(ms)'] = np.where(df1['Temps_ms'] - io <0, df1['Temps_ms'] + 60 - io, df1['Temps_ms'] -io)

    df1=df1.drop(columns=['Temps_ms'])#to eliminate column Temps_ms

    df1=df1[['Date','NoBille','Heure','Temps(ms)','Force(N)','Vitesse(RPM)','Puissance(w)','Torque(N/m)','Angle(deg)']]# to reorder the final dataframe

    ##############################################################################
    print(df1.head())

    df1.to_csv('data1.txt', index = False)

1 个答案:

答案 0 :(得分:0)

如果此脚本可以应用于“输入文件夹”中的所有数据库,则可以将数据库名称作为参数传递给python脚本:

import sys

#...your other imports...

### take the first argument passed in commmand line as db_name and second as output_file name
db_name = sys.argv[1]
output_filename = sys.argv[2]
df1 = pd.read_csv(db_name, sep='\s+') 

#...rest of your script...

df1.to_csv(output_filename, index = False)

,然后yuo可以使用外部循环(例如ex)轻松地在输入目录中的文件上调用python脚本。在bash中:

mkdir <output-dir>
ls <input-dir> | while read file; do
    output_filename="<output-dir>/$file.output.txt"
    python <your-script-name> $file $output_filename
done

它会自动获取文件,操作python脚本并以指定的扩展名将它们记录在output-dir中