简体   繁体   中英

permission denied when creating an .html file in python

so this is my first python experience. I have a list of images in folder that I'm trying to convert to html pages. For that I have the following code:

import inspect, os, errno, markup
path =  os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))+'/www/img/'
print path

for f in os.listdir(path):
  counter = 1
  page = markup.page()
  page.init(charset="UTF-8")
  from markup import oneliner as e
  page.a(e.img(src='img/'+f, width=1024, height=768), href='')
  final = open('/index'+str(counter)+'.html','w')
  final.write(page)

and I get an IOError: [Errno 13] Permission denied: '/index1.html' message.... any clues or ideas are much appreciated. thanks!

I think the main problem is related to file permissions of root folder ( / ). Try running the script as root or pick some other directory that you are sure you have write access to.

Your problem is here: open('/index'+str(counter)+'.html','w')

The path starting with '/' is an absolute path, no matter what's your current directory. And it's not a python specific.

Assuming you're using Unix/Linux (by the forward slashes), any path that starts with a / is absolute. So you're trying to write a file named index.html to the root of your file system. Only root can do that.

If you want the path to be relative, change:

final = open('/index'+str(counter)+'.html','w')

to:

final = open('index'+str(counter)+'.html','w')

Notice the missing / in front of index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM