我正试图实现一个python脚本,让我根据给定给脚本的参数生成一个文件“file\u modified”
我的脚本运行得很好,但现在我想实现一个类似于“rm-r”的“递归”选项:目标是为我的脚本提供一个文件夹(包含n个文件)作为参数,并基于这个文件夹生成n个新文件。你知道吗
#!/usr/bin/python
''' Python tools to blabla
'''
import argparse
from datetime import datetime
import sys
import pandas as pd
EXTENSION="_friendly_excel"
FILETYPE=".csv"
def get_args():
'''This function parses and return arguments passed in'''
# Assign description to the help doc
parser = argparse.ArgumentParser(
description='python command to transform a former LPBM .csv file to an excel friendly one')
# Add arguments
parser.add_argument(
"file_csv", help="file you wish to convert")
parser.add_argument('-f', "--filename", type=str, required=False, help="specify a name for the output file")
# Assign args to variables
args = parser.parse_args()
file_csv=args.file_csv
filename=args.filename
# Return all variable values
return file_csv, filename
if __name__ == "__main__":
# Run get_args()
# get_args()
# Match return values from get_arguments()
# and assign to their respective variables
file_csv, filename = get_args()
#name of the file :
if filename:
filename=filename+FILETYPE
else:
filename=file_csv[:-4:]+EXTENSION+FILETYPE
# Print the values
print "\nfile you wish to convert : %s \n" % file_csv
#opening the file as a dataframe
try:
df = pd.read_csv(file_csv, sep=';', parse_dates=['date-time'])
except:
print "\nfailed to load the dataframe : are you sure your file is an LPBM generated one ?\n"
exit()
#saving the modified dataframe with correct date format and decimal
df.to_csv(filename, date_format='%d/%m/%Y %H:%M:%S', sep=';', decimal=',', index=False)
print "le fichier %s a bien ete cree" % filename
我用这个简单的代码片段来处理我的大部分回避
相关问题 更多 >
编程相关推荐