如何将映像从服务器目录复制到python中的另一个目录

时间:2015-07-30 20:34:44

标签: python python-2.7 cgi shutil

我正在尝试从服务器上的目录复制图像,并将其粘贴到服务器上的另一个文件中。然后我想重命名它。

这是我的代码(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)

1 个答案:

答案 0 :(得分:1)

我不知道你正在尝试什么(并询问)但是你需要使用相对或绝对路径来代替网址。

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