我想使用Python将所有文本文件从一个文件夹移动到另一个文件夹.我找到了这段代码:
import os, shutil, glob
dst = '/path/to/dir/Caches/com.apple.Safari/WebKitCache/Version\ 4/Blobs '
try:
os.makedirs(/path/to/dir/Tumblr/Uploads) # create destination directory, if needed (similar to mkdir -p)
except OSError:
# The directory already existed, nothing to do
pass
for txt_file in glob.iglob('*.txt'):
shutil.copy2(txt_file, dst)
Run Code Online (Sandbox Code Playgroud)
我希望它移动文件Blob夹中的所有文件.我没有收到错误,但它也没有移动文件.
我用for循环编写了一些代码.现在我想在执行循环一次后停止for循环.这是for循环:
for table in tables: #reason for it to do it 12x
for tr in table.find_all("tr"):
firstTd = tr.find("td")
if firstTd and firstTd.has_attr("class") and "indent" in firstTd['class']:
values = {}
tds = tr.find_all("td")
maxVal = tds[1].find("span", class_="wx-value")
avgVal = tds[2].find("span", class_="wx-value")
minVal = tds[3].find("span", class_="wx-value")
if maxVal:
values['max'] = maxVal.text
if avgVal:
values['avg'] = avgVal.text
if minVal:
values['min'] = minVal.text
if len(tds) > 4:
sumVal = tds[4].find("span", class_="wx-value")
if sumVal:
values['sum'] = sumVal.text
scrapedData = {}
scrapedData[firstTd.text] = values
weatherdata.append(scrapedData) …Run Code Online (Sandbox Code Playgroud)