Reputation: 143
I am trying to copy an image from a directory on my server and paste is to another file on server. Then I am looking to rename it.
Here is my code (New to python)
#! /usr/bin/python
import cgi
import os
import shutil
print "Content-type: text/html\r\n\r\n"
username = 'bytesized'
srcfile = 'http://test.com/img/default/basicprof.jpg'
dstroot = 'http://test.com/img/%s.jpg' % username
assert not os.path.isabs(srcfile)
dstdir = os.path.join(dstroot, os.path.dirname(srcfile))
os.makedirs(dstdir)
shutil.copy(srcfile, dstdir)
Upvotes: 0
Views: 1690
Reputation: 7026
I don't know what you are exactly trying (and asking) but you need to use relative or absolute paths to your image instead of urls.
currentpath = '/img/default/basicprof.jpg'
newpathandname = '/img/{}.jpg'.format(username)
os.rename(currentpath, newpathandname) #does not keep original file
shutil.copyfile(currentpath, newpathandname) #keeps original file
Upvotes: 3