Opening a CSV from a Different Directory Python

17,349

The tilde tells me that this is the home folder (e.g. C:\Users\<username> on my Windows system, or /Users/<username> on my Mac). If you want to expand the user, use the os.path.expanduser call:

full_path = os.path.expanduser('~/Projects/bmm_private/login_test.txt')
# Now you can open it

Another approach is to seek for the file in relative to your current script. For example, if your script is in ~/Projects/my_scripts, then:

script_dir = os.path.dirname(__file__)  # Script directory
full_path = os.path.join(script_dir, '../bmm_private/login_test.txt')
# Now use full_path
Share:
17,349
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I've been working on a project where I need to import csv files, previously the csv files have been in the same working directory. Now the project is getting bigger so for security and organizational resaons I'd much prefer to keep them in a different directory.

    I've had a look at some other questions asking similar things but I couldn't figure out out how to apply them to my code as each time I tried I kept getting the same error message mainly:

    IOError: [Errno 2] No such file or directory:
    

    My original attempts all looked something like this:

    import csv      # Import the csv module
    import MySQLdb  # Import MySQLdb module
    
    def connect():
    
        login   = csv.reader(file('/~/Projects/bmm_private/login_test.txt'))
    

    I changed the path within several times as well by dropping the first / then then the ~ then the / again after that, but each time I got the error message. I then tried another method suggested by several people by importing the os:

    import os
    import csv      # Import the csv module
    import MySQLdb  # Import MySQLdb module
    
    def connect():
    
        path    = r'F:\Projects\bmm_private\login_test.txt'
        f       = os.path.normpath(path)
        login   = csv.reader(file(f)) 
    

    But I got the error message yet again.

    Any help here would be much appreciated, if I could request that you use the real path (~/Projects/bmm_private/login_test.txt) in any answers you know of so it's very clear to me what I'm missing out here.

    I'm pretty new to python so I may struggle to understand without extra clarity/explanation. Thanks in advance!