Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中按特定顺序重命名本地文件夹中的多个文件_Python_File_Split_Multiprocessing_Rename - Fatal编程技术网

在python中按特定顺序重命名本地文件夹中的多个文件

在python中按特定顺序重命名本地文件夹中的多个文件,python,file,split,multiprocessing,rename,Python,File,Split,Multiprocessing,Rename,我想按特定顺序重命名多个python文件: 我当前的输出文件名是 e.g. https___c.tile.opentopomap.org_16_j_i (e.g. https___c.tile.opentopomap.org_16_34309_22369) 但是想要的输出文件名称应该如下所示: e.g. https___c.tile.opentopomap.org_16_i_j (e.g. https___c.tile.opentopomap.org_16_22369_34309) 到目

我想按特定顺序重命名多个python文件:

我当前的输出文件名是

e.g. https___c.tile.opentopomap.org_16_j_i (e.g. https___c.tile.opentopomap.org_16_34309_22369) 
但是想要的输出文件名称应该如下所示:

e.g. https___c.tile.opentopomap.org_16_i_j (e.g. https___c.tile.opentopomap.org_16_22369_34309) 
到目前为止,我在代码中没有看到错误:

import requests
import multiprocessing
import pprint
import time

print("The pictures will be saved all what is east-south from Mannheim Quadrate")
x2 = int(input("North-South (latitude) (start: 22369): "))
x3 = int(input("East-West (longitude) (start: 34309): "))
urls = [
    f"https://c.tile.opentopomap.org/16/{j}/{i}.png" for i in range(22369, x2 + 1) for j in range(34309, x3 + 1)]

def download_image(url):
    response = requests.get(url)
    splitted_url = url.split("/")
    last_part = splitted_url[-1].replace(".png", "")
    second_to_the_last = splitted_url[-2]
    splitted_url[-1] = second_to_the_last
    splitted_url[-2] = last_part

    print(f"Downloading from {url}...")
    url = url.replace("/", "_").replace(":", "_")
    with open(f"{url}", "wb") as file:
        file.write(response.content)

    print(f"Downloading from {url}...")
    url = url.replace("/", "_").replace(":", "_")
    with open(f"{url}", "wb") as file:
        file.write(response.content)

if __name__ == "__main__":
    start = time.perf_counter()
    p = multiprocessing.Pool(processes=4)
    p.map(download_image, urls)
    p.close()
    stop = time.perf_counter()


    print(f"It took {round(stop - start, 2)} seconds in total")

该部分应完成以下工作:

    splitted_url = url.split("/")
    last_part = splitted_url[-1].replace(".png", "")
    second_to_the_last = splitted_url[-2]
    splitted_url[-1] = second_to_the_last
    splitted_url[-2] = last_part
但它不起作用。文件名的顺序仍然错误(例如https\uuuuu c.tile.opentopomap.org\u 16\u 34309\u 22369


我不知道为什么它不切换文件名的最后部分。有人知道吗?

为什么不切换I和j

URL=[
f“https://c.tile.opentopomap.org/16/{j} /{i}.png“表示范围内的i(22369,x2+1)表示范围内的j(34309,x3+1)]

变成:


url=[f”https://c.tile.opentopomap.org/16/{i} /{j}.png“对于范围内的i(22369,x2+1)对于范围内的j(34309,x3+1)]
我想你是想把拆分的url重新连接在一起:

splitted_url[-1] = second_to_the_last
splitted_url[-2] = last_part
url = '_'.join(splitted_url)    # join the parts together

print(f"Downloading from {url}...")
url = url.replace("/", "_").replace(":", "_")
with open(f"{url}", "wb") as file:
    file.write(response.content)

您可以:splitted_url=url.split(“/”),但以后继续使用“url”而不进行更改。另外,为什么要向文件写入两次?您好,我处理了您的评论,并将“url=url.replace(“/”,”).replace(“:”,”)替换为“splitted_url=url.replace(“/”,”).replace(“:”,”).replace(“,”)”但是文件名的结果是:https\uuuuuu c.tile.opentopomap.org\u 16\u 34309\u 22369而不是https\uuuuuuu c.tile.opentopomap.org\u 16\u 22369\u 34309。很高兴收到您的帮助。您好,谢谢您的评论,下载部分是正确的。如果我切换i和j,则png是错误的。我只需要在下载后切换文件名的最后部分以获取更多信息ther processing.Hi,它的工作原理是包含一行:“url=url+'.png'”以将png作为输出文件!太棒了,谢谢