我正在尝试制作一个程序,它将打开一个目录,然后使用正则表达式获取 powerpoint 的名称,然后在本地创建文件并复制其内容。当我运行它时,它似乎可以正常工作,但是当我实际尝试打开文件时,他们一直说版本错误。
from urllib.request import urlopen
import re
urlpath = urlopen('http://www.divms.uiowa.edu/~jni/courses/ProgrammignInCobol/presentation/')
string = urlpath.read().decode('utf-8')
pattern = re.compile('ch[0-9]*.ppt') #the pattern actually creates duplicates in the list
filelist = pattern.findall(string)
print(filelist)
for filename in filelist:
remotefile = urlopen('http://www.divms.uiowa.edu/~jni/courses/ProgrammignInCobol/presentation/' + filename)
localfile = open(filename,'wb')
localfile.write(remotefile.read())
localfile.close()
remotefile.close()
最佳答案
这段代码对我有用。我只是对其进行了一些修改,因为您正在复制每个 ppt 文件。
from urllib2 import urlopen
import re
urlpath =urlopen('http://www.divms.uiowa.edu/~jni/courses/ProgrammignInCobol/presentation/')
string = urlpath.read().decode('utf-8')
pattern = re.compile('ch[0-9]*.ppt"') #the pattern actually creates duplicates in the list
filelist = pattern.findall(string)
print(filelist)
for filename in filelist:
filename=filename[:-1]
remotefile = urlopen('http://www.divms.uiowa.edu/~jni/courses/ProgrammignInCobol/presentation/' + filename)
localfile = open(filename,'wb')
localfile.write(remotefile.read())
localfile.close()
remotefile.close()
关于python - urllib 下载在线目录的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10875215/