0

I have the following code, and I run it from localhost:

def create_names_file(req, names, data, profileid):
    s = names
    fname = str(profileid)
    fpath = req.conf["inf_path"]+"/"+fname
    f = open(fpath, 'w')

req.conf["inf_path"] is /opt/fp/trunk/analysis/2/, and I receive permission error.I use Ubuntu OS. How can I solve this problem?

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
yusuf
  • 3,591
  • 8
  • 45
  • 86
  • 1
    Run your script as `root` maybe can solve this. – Remi Guan Dec 19 '15 at 13:10
  • Actually I'm running on localhost. – yusuf Dec 19 '15 at 13:11
  • 1
    I know, do you have an user on your OS called `root`? or you can use command `sudo python your_script_name.py` instead of `python your_script_name.py`. – Remi Guan Dec 19 '15 at 13:12
  • I open localhost url from browser. There should be a different solution for that. It is so weird – yusuf Dec 19 '15 at 13:16
  • 1
    @yusuf, you simply don't have permissions to read the file, what does `ls -l` on the file output? https://help.ubuntu.com/community/FilePermissions – Padraic Cunningham Dec 19 '15 at 13:18
  • 1
    Then you could change the permission of `fpath`...whatever, take a look at [Red Hat Enterprise Linux Step By Step Guide: 4.11. Ownership and Permissions](https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/4/html/Step_by_Step_Guide/s1-navigating-ownership.html) maybe can give you some ideas. – Remi Guan Dec 19 '15 at 13:19
  • 2
    @KevinGuan, how would opening a directory give you a permissions error? You would get `IOError: [Errno 21] Is a directory:` – Padraic Cunningham Dec 19 '15 at 13:26
  • Yes Kevin, I want to do that. – yusuf Dec 19 '15 at 13:27
  • @PadraicCunningham: Wait, my bad. The `fpath` actually is `req.conf["inf_path"]+"/"+fname`. And the directory actually is `req.conf["inf_path"]` . (I've edited the question to clarify it.) – Remi Guan Dec 19 '15 at 13:28
  • 2
    @yusuf, you need to add the full permissions to your question, if you don't want to run the script with sudo you will have to give your user read permissions on the file – Padraic Cunningham Dec 19 '15 at 13:31
  • I have tried to do that, but I couldn't manage Padraic. Could you give me a little help? – yusuf Dec 19 '15 at 13:33
  • 1
    @yusuf, add what `ls -l your_file` outputs on the file, obviously replacing the your_file with the full path to the file – Padraic Cunningham Dec 19 '15 at 13:35
  • the output is "total 0" – yusuf Dec 19 '15 at 13:37
  • How can I do it Padraic? :) – yusuf Dec 19 '15 at 13:41
  • 1
    You can `sudo chown username folder` then `sudo chmod u+rw folder`, that will change the owner of the dir to username, then the second command gives that user read and write permissions – Padraic Cunningham Dec 19 '15 at 14:08

1 Answers1

1

You seem to be trying to open a file named /opt/fp/trunk/analysis/2/ which in invalid due to the trailing slash. Possibly that is a typo so, if the required file already exists, who owns it?

Does the user that you run Python as have permissions to write to that file?

Check the permissions reported by ls -l /opt/fp/trunk/analysis/2.

mhawke
  • 84,695
  • 9
  • 117
  • 138
  • the output is "total 0" – yusuf Dec 19 '15 at 13:26
  • Then `/opt/fp/trunk/analysis/2` represents an empty directory. So, unless you intending to open the directory file itself, there is nothing to open. Also, you don't have permission to open the directory, You can check the permissions for that with `ls -ld /opt/fp/trunk/analysis/2`. – mhawke Dec 19 '15 at 14:21