复制到python中的另一个目录时如何重命名文件?

问题描述

我想将文件一个目录复制到另一个目录,同时在python中重命名文件。需要将“文件”复制为“新文件”而不重命名源目录中的文件。我尝试过这样的事情;

     import shutil,os
     src_path="path of source directory"
     dst="path of destination directory"
     for file in os.listdir(src):
         file_path=os.path.join(src_path,file)
          shutil.copy(file_path,dst/"new"+'-'+file)

但是这不起作用。我知道通过使用os.rename()模块,可以在复制后重命名它。但是我有一些名称相似的文件,它们将替换已经复制的文件,以避免出现需要将每个文件重命名为“新文件”以及复制自身的情况。

感谢您的帮助。预先感谢

解决方法

您尝试使用var table = document.getElementById("myTableJob"); var tr = table.querySelectorAll('tr'); function myFunction(event) { var input,filter,td,i; input = document.getElementById("myInputJob"); filter = input.value.toUpperCase(); for (i = 0; i < tr.length; i++) { table.style.display = "" if (document.getElementById('myInputJob').value == '') { table.style.display = ""; } else { table.style.display = ""; } td_1 = tr[i].getElementsByTagName("td")[2]; td_2 = tr[i].getElementsByTagName("td")[3]; td_3 = tr[i].getElementsByTagName("td")[1]; if (td_1 || td_2) { if (td_1.innerHTML.toUpperCase().indexOf(filter) == 0 || td_2.innerHTML.toUpperCase().indexOf(filter) == 0 && td_3.innerHTML == document.getElementById("StateDropdown").innerHTML) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } function filterTable() { let dropdown,rows,cells,state,filter; dropdown = document.getElementById("StateDropdown"); rows = table.getElementsByTagName("tr"); filter = dropdown.value; for (let row of rows) { cells = row.getElementsByTagName("td"); state = cells[1] || null; if (filter === "All" || !state || (filter === state.textContent)) { row.style.display = ""; } else { row.style.display = "none"; } } tr = table.querySelectorAll('tr:not([style="display: none;"])'); }将字符串连接在一起的尝试将失败,因为这是在尝试进行除法。在为源文件创建完整路径时,您已经正确使用了<div id="container"> <div id="Header"> </div> <div id="MainBody"> <h2>Search</h2> <input type="text" id="myInputJob" onkeyup="myFunction()" placeholder="" title="Type Here"> <select id="StateDropdown" oninput="filterTable()"> <option>All</option> <option>CA</option> <option>WA</option> </select> <div id="TableJob"> <table id="myTableJob"> <tr class="header"> <th>City</th> <th>State</th> <th>Keyword1</th> <th>Keyword2</th> </tr> <tr> <td>San Francisco</td> <td>CA</td> <td>Gray</td> <td>Red</td> </tr> <tr> <td>San Diego</td> <td>CA</td> <td>Blue</td> <td>Red</td> </tr> <tr> <td>Seattle</td> <td>WA</td> <td>Blue</td> <td>Red</td> </tr> <tr> <td>Salt Lake City</td> <td>UT</td> <td>Blue</td> <td>Red</td> </tr> <tr> <td>Las Vegas</td> <td>NV</td> <td>Blue</td> <td>Pink</td> </tr> </table> </div> </div>,并且只需要对目标文件执行相同的操作即可。

dst/"new"
,

每当您想在Python 3中操作路径时,我觉得您应该接触pathlib。这是将shututil的{​​{3}}与使用pathlib的自定义复制功能一起使用的解决方案。很好,因为它也可用于嵌套目录-请注意,它不会重命名目录,而只会重命名文件:

from pathlib import Path
import shutil


def copy_and_rename(src: str,dst: str):
    """copy and rename a file as new-<name>"""
    new_name = "new-" + Path(dst).name
    new_dst = Path(dst).with_name(new_name)
    shutil.copy2(src,new_dst)


shutil.copytree(
    "./copy-from-me","./copy-to-me",copy_function=copy_and_rename,dirs_exist_ok=True
)

这是运行此示例:

$ tree copy-from-me
copy-from-me
├── 1.txt
├── 2.txt
└── nested
    └── 3.txt

$ tree copy-to-me
copy-to-me
├── nested
│   └── new-3.txt
├── new-1.txt
└── new-2.txt