将当前在%Y%m%d%H%M%S中的多个文件重命名为Python中的unix纪元时间

时间:2017-10-10 00:33:55

标签: python unix-timestamp

我需要从%Y%m%d%H%M%S转换数百个文件名,例如' 20170125211201' unix epoch时间。我能够使用

对单个文件执行此操作
>>> import os
>>> f='20170125211201'
>>> import time 
>>> date_time='20170125204151'
>>> pattern='%Y%m%d%H%M%S'
>>> epoch=int(time.mktime(time.strptime(date_time,pattern)))
>>> new_name='{}'.format(epoch)
>>> os.rename(f,new_name)

我仍然需要对具有相同文件模式的数百个其他文件执行相同的操作但没有运气。 我真的很感激任何帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码段解决您的任务:

import pandas as pd
import numpy as np
from random import seed
# Load scikit's random forest classifier library
from sklearn.ensemble import RandomForestClassifier

from sklearn.model_selection import train_test_split
seed(1) ### <-----

file_path = 'https://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data'
dataset2 = pd.read_csv(file_path, header=None, sep=',')

from sklearn import preprocessing
le = preprocessing.LabelEncoder()

#Encoding
y = le.fit_transform(dataset2[60])
dataset2[60] = y
train, test = train_test_split(dataset2, test_size=0.1)
y = train[60] 
y_test = test[60] 
clf = RandomForestClassifier(n_jobs=100, random_state=0)
features = train.columns[0:59] 
clf.fit(train[features], y)

# Apply the Classifier we trained to the test data
y_pred = clf.predict(test[features])

# Decode 
y_test_label = le.inverse_transform(y_test)
y_pred_label = le.inverse_transform(y_pred)


from sklearn.metrics import accuracy_score
print (accuracy_score(y_test_label, y_pred_label))

# Two following results:
# 0.761904761905
# 0.90476190476