How to open a file on mac OSX 10.8.2 in python

后端 未结 3 1686
囚心锁ツ
囚心锁ツ 2021-01-29 10:18

I am writing a python code on eclipse and want to open a file that is present in Downloads folder. I am using MAC OSX 10.8.2. I tried with f=os.path.expanduser(\"~/Downloa

3条回答
  •  死守一世寂寞
    2021-01-29 11:00

    It is most likely a permissions issue, if you try your code in the Python interpreter you will probably receive a "Permission denied" error from the shell when you call subprocess.Popen. If this is the case then you will need to make the file a minimum of 700 (it's probably 644 by default) and you'll probably want 744.

    Try the code in the Python interpreter and check for the "Permission denied" error, if you see that then do this in a shell:

    chmod 744 ~/Downloads/DeletingDocs.txt
    

    Then run the script. To do it all in Python you can use os.system:

    import os
    import subprocess
    
    filename = "~/Downloads/DeletingDocs.txt"
    os.system("chmod 744 "+filename)
    ss=subprocess.Popen(filename, shell=True)
    ss.communicate()
    

    The reason it "just works" in Windows is because Windows doesn't support file permission types (read, write and execute) in the same way as *nix systems (e.g. Linux, BSD, OS X, etc.) do.

提交回复
热议问题