Reading a file in home directory from another directory

前端 未结 4 1063
忘了有多久
忘了有多久 2021-01-27 11:18

I am trying to read a file in another directory. My current script is in path/to/dir. However, I want to read a file in ~/. I\'m not sure how to do thi

相关标签:
4条回答
  • 2021-01-27 11:51

    This should work

    from os.path import expanduser
    home = expanduser("~")
    
    0 讨论(0)
  • 2021-01-27 12:04

    The short answer is: use os.path.expanduser, as m.wasowski shows:

    f = open(os.path.expanduser("~/.file"))
    

    But why do you have to do this?

    Well, ~/.file doesn't actually mean what you think it does. It's just asking for a file named .file in a subdirectory named ~ in the current working directory.

    Why does this work on the command line? Bcause shells don't just pass your arguments along as typed, they do all kinds of complicated processing.

    This is why ./myscript.py *txt gives you ['./myscript.py', 'a.txt', 'b.txt', 'c.txt', 'd e f.txt'] in your argv—because the shell does glob expansion on *.txt by looking in the directory for everything that matches that pattern and turns it into a.txt b.txt c.txt "d e f.txt".

    In the same way, it expands ~/.file into /Users/me/.file by looking at the appropriate environment variables and platform-specific defaults and so on. You can read the gory details for bash; things are similar with dash, tcsh, and most other shells (except Windows cmd and PowerShell).

    Python talks to the filesystem directly, it doesn't go through the shell to do it. So, if you want to do the same things shells do, you have to do them explicitly: glob.glob, os.path.expanduser, os.path.expandvars, subprocess.check_output, etc.

    The cool thing about doing it all in Python is that it works the same way everywhere. Even on Windows, where the shell doesn't know what ~/.file means, and the right answer may be some hideous thing like 'C:\\Documents and Settings\\me\\Documents' or '\\\\FileServer\\RoamingProfiles\\me', os.path.expanduser will give you the right hideous thing.

    0 讨论(0)
  • 2021-01-27 12:06

    use os.path.expanduser:

    import os
    with open(os.path.expanduser('~/.file')) as f:
        print f.read()
    
    0 讨论(0)
  • 2021-01-27 12:11

    os.path.expanduser will help you

    0 讨论(0)
提交回复
热议问题