Import into Python a CSV File that has a Variable Name

In this post, I’m going to 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

We will review a simple example where you’ll be able to:

  • Create a simple graphical user interface (GUI) with an input box. You’ll be able to type your variable (such as a specific date) in the input box
  • The GUI will also contain a single button. Once you click on that button, the CSV file will be imported into Python based on the variable that you typed

To accomplish those tasks, you’ll need to import the tkinter package (used to create the GUI). You will also need to import 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\Doron E\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 orange 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 quotations (while the non-variable portions, in blue and orange, should be placed within quotations)

Let’s say that you have the following data in the CSV file. And that the CSV file name contains the date of 11032018 (this date is your variable), such that the full CSV file name is: Sales_11032018


Client NameCountryProductPurchase PriceDate
Jon SmithJapanComputer$80011-Mar-18
Bill MartinUSTablet$45011-Mar-18
Maria BlueCanadaPrinter$15011-Mar-18
Rita YuBrazilLaptop$1,20011-Mar-18
Jack MoUKMonitor$30011-Mar-18


To import into Python the CSV file that has a variable name, you can use the following Python code (as indicated above, you’ll need to change the path name to correspond to the name where your CSV file is stored on your computer).

I also included some additional notes (within the code) to explain each component of the Python code:


import pandas as pd
from pandas import DataFrame
import tkinter as tk 
root= tk.Tk()
canvas1 = tk.Canvas(root, width = 500, height = 350) # create the canvas (tkinter module)
entry1 = tk.Entry (root) # create 1st entry box
canvas1.create_window(100, 200, window=entry1) # create a canvas window to place the 1st entry box on top of the background image
def insert_number(): # create 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\Doron E\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) # button to call the 'insertnumber' command above 
canvas1.create_window(140, 220, window=button1) # create a canvas window to place button1 on top of the background image


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

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 data-set. Make sure that the number of columns assigned using the data-frame matches with the number of columns within your CSV file.

Running the Code in Python

(1) Press F5 to run the python code

(2) Type the date of 11032018

(3) 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 12032018. The full CSV file name would then be: Sales_12032018


Client NameCountryProductPurchase PriceDate
Rick JonesSpainComputer$80012-Mar-18
Eva GreenUSTablet$45012-Mar-18


In that case, type the date of 12032018 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:


import into Python a CSV file that has a variable name


Mission accomplished!


We just saw how to import into Python a CSV file that has a variable name. In this post, we 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.