将相同的.py文件复制到不同的文件夹中

时间:2018-04-19 12:55:21

标签: cmd copy

我想在 cmd 中执行一项简单的任务。我有一个名为Export_Excel.py的python文件存储在D:\Getting_Started_PyCharm\pyradiomics中,我希望将其复制到许多目录中。

更具体地说,我有30个目录位于名为NRRD的文件夹中,它们被称为:

1_patient, 2_patient, ..., 30_patient

有没有办法用 for 循环和cmd中的复制函数

执行此操作

提前多多感谢

2 个答案:

答案 0 :(得分:0)

rem You should replace "" with NRRD's path in the file
set temppath=""
FOR /R %temppath% %%G in (.) DO (
 Pushd %%G
 copy D:\Getting_Started_PyCharm\pyradiomics\Export_Excel.py %%G
 Popd )
echo Copying is done.

PUSHD和POPD的例子

   C:\Program Files> PUSHD c:\utils
   C:\utils> PUSHD c:\Windows
   C:\Windows> 
   C:\Windows> POPD
   C:\utils> 
   C:\utils> POPD
   C:\Program Files>

这是更新后的代码:  将此文件保存在记事本中作为forcopy.bat并从文件夹中运行它。

set temppath=C:\Users\tommaso\Desktop\Lavoro\Zurich_Fellowship\Myocardial_Infarction_DICOM_Images_19_04 _Copia\NRRD
cd %temppath%
for /r %temppath% %%G IN (.) DO (
Pushd %%G
copy "C:\Users\tommaso\Desktop\Excel_Export.py" %%G
Popd
)
del Excel_Export.py
cls
Echo Copying is done
pause
rem //Pause is to pause the execution and wait until the user presses a key.//

答案 1 :(得分:0)

最后,我找到了一个更简单的python替代品:

import os
from shutil import copy

path_to_file = "/.../.../Export_Excel.py"

path_destination_folder = "/.../.../NRRD/"

for sub_folders in os.listdir(path_destination_folder):
    copy(path_to_file, os.path.join(path_destination_folder, sub_folders))