我正在尝试从服务器上的目录复制图像,并将其粘贴到服务器上的另一个文件。然后我想重新命名它。
这是我的代码(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)
我不知道你到底在尝试什么(和问什么),但你需要使用图像的相对或绝对路径,而不是URL。
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