2017-09-14 50 views
-3

我正在寫大函數,我需要使用輸入文件名來輸出輸出文件名。我嘗試了一些東西如何使用pandas.read_csv導入文件時獲取文件名

import pandas as pd 
import os 
input_file = pd.read_csv('my_file.csv',header=None) 
input_file_name = os.basename(input_file) 

但我無法取回文件名。 如何在這裏檢索'my_file'?

回答

0
def do_job(input_file): 
    if not os.path.exists(input_file): 
     sys.stderr.write("Error: '%s' does not exist"%input_file) 
     sys.exit(1) 
    input = pd.read_csv(input_file,header=None) 

    # do many operations 

    # so file name is stored in handle 'input_file' 
    # I can give output file name using input_file 
    output_name = 'Results_' + input_file 
相關問題