Search code examples
pythondjangopython-2.7shelldjango-shell

Grab file from Desktop in Django Shell


I have an app named cars inside my Django project that has a utils.py file that contains various utility methods used by the application. One of them (grab_new_models) is used to process a CSV file that's normally picked up via a periodic tasks that fetches the file from a remote location. The method itself is passed the CSV file itself normally so that the method looks like

def grab_new_models(csv_file):

Right now I'm trying to update the code with some new functionality and having issues testing it locally. Using the Django shell, I can't figure out how to pass the file into the method to test it. I have a copy of the csv file on my Desktop. How do I call this method from the Django shell and pass it my local csv file?


Solution

  • So you are on the Django shell, which is python shell with some extra imports. You can just import your util and call your function. Below I provide a simple example, which reads a simple file from my desktop and prints it:

    Method:

    def grab_new_models(csv_file):
        with open(csv_file) as f1:
            a = f1.read()
            print(a)
    

    Execution:

    grab_new_models(r'C:\Users\L3\Desktop\learn_note.txt')