Import into Python a CSV File that has a Variable Name

In this guide, I’ll show you how to import into Python a CSV file that has a variable name. I’ll use a simple example to illustrate this concept, as well as provide the full Python code.

The Goal

Let’s review a simple example where you’ll be able to:

To accomplish the above goals, you’ll need to import the tkinter package (used to create the GUI) and the pandas package (used to import the CSV file into Python).

This is how the GUI would look like once you apply the Python code:

Import into Python a CSV File that has a Variable Name

Example – Import into Python a CSV File that has a Variable Name

Let’s say that you want to import into Python a CSV file, where the file name is changing on a daily basis.

For instance, the CSV file name may contain a date, which varies each day.

In my case, I stored the CSV file under the path below. Note that you’ll need to change this path (in the Python code to be provided below) to the location where the CSV file is stored on your computer.

‘C:\Users\Ron\Desktop\Import into Python\Sales_’ + x1 + ‘.csv’


  • The portion highlighted in blue is the part of the path name that never changes
  • The portion in green represents the variable (in our case, it is the date)
  • The portion in purple is the CSV file extension which also doesn’t change
  • Note that the + signs are used to concatenate the different components of the path name. Also note that the variable (in green) should never be placed within quotes (while the non-variable portions, in blue and purple, should be placed within quotes)

Let’s say that you have the following data stored in a CSV file. And that the CSV file name contains the date of 27042019 (this date is your variable), so the full CSV file name is: Sales_27042019

Client NameCountryProductPurchase PriceDate
Jon SmithJapanComputer$80027-Apr-19
Bill MartinUSTablet$45027-Apr-19
Maria BlueCanadaPrinter$15027-Apr-19
Rita YuBrazilLaptop$1,20027-Apr-19
Jack MoUKMonitor$30027-Apr-19

To import into Python the CSV file that has a variable name, you can use the Python code below (as already mentioned, you’ll need to change the path name to reflect the location where the CSV file is stored on your computer).

Additional notes are included within the code:

import pandas as pd
from pandas import DataFrame
import tkinter as tk 
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 300, height = 300) # create the canvas
entry1 = tk.Entry (root) # create the entry box
canvas1.create_window(150, 100, window=entry1)
def insert_number(): # add a function/command to be called by the button (i.e., button1 below)
    global x1 # add 'global' before the variable x1, so that you can use that variable outside of the command/function if ever needed 
    x1 = str(entry1.get()) # store the data input by the user as a variable x1 
    PATH = r'C:\Users\Ron\Desktop\Import into Python\Sales_' + x1 + '.csv' #(use "r" before the path string to address special character, such as '\'). Don't forget to put the file name at the end of the path + '.csv'
    read_sales = pd.read_csv (PATH)   #read the csv file using the 'PATH' varibale 
    df = DataFrame(read_sales,columns=['Client Name','Country','Product','Purchase Price','Date'])  # assign column names
    print (df)
button1 = tk.Button (root, text='Input date to import file (ddmmyyyy) ',command=insert_number, bg='green', fg='white') # button to call the 'insert_number' command above 
canvas1.create_window(150, 140, window=button1)

Note that in the Python code, we assigned the column names (highlighted in yellow) using the DataFrame:

df = DataFrame(read_sales,columns=[‘Client Name’,’Country’,’Product’,’Purchase Price’,’Date’])

If your CSV file contains different column names, and/or different number of columns, you’ll need to change the yellow portion to match with your dataset.

Running the Code in Python

(1) First, run the code in Python

(2) Then, type the date of 27042019

(3) Finally, click on the button ‘Input date to import file (ddmmyyyy)

Import into Python a CSV File with Variable Name

This is the result that you’ll get:

Import into Python

Let’s say that in the following day, you got a new CSV file, where the date is 28042019. The full CSV file name would then be: Sales_28042019

Client NameCountryProductPurchase PriceDate
Rick JonesSpainComputer$80028-Apr-19
Eva GreenUSTablet$45028-Apr-19

In that case, type the date of 28042019 in the entry box:

How to import into Python a CSV file that has a variable name

Then, click on the button ‘Input date to import file (ddmmyyyy)‘ and you should see the next-date results:

Python dataframe

Mission accomplished!


You just saw how to import into Python a CSV file that has a variable name. In this guide, you used a date variable. However, you can apply the same concepts for different types of variables.

Additionally, similar principles would apply if you try to import different file types, such as in the case of Excel files.