将指定目录下指定数目的目录分批移动到另一目录

情景1:

现有一个目录dir_origin,该目录下有10000个子目录。现需要将该目录下的10000子目录,100个子目录为一批,分别移动到指定目录:1~100子目录移动到dir1,101~102子目录移动到dir2。只关注数目,不关注文件命名方式。

 

方法:

1. 用脚本创建100个格式化名称的dirNUM

#!/bin/bash
# make sure you route of cp_$1 is right

function getfiles(){
    for ((i=1; i<=10; i ++))
    do
        `mkdir cp_$i`
    done
}

getfiles $1

2.分批移动目录vim 

#!/bin/bash
# make sure  mv $1/$file cpdir/cp_$count_out have a right route


count_in=0
count_out=0

function getfiles(){
  for file in `ls $1`
  do
    if [ $count_in -eq 10 ]
    then
      ((count_out++))
      count_in=0
    fi
    mv $1/$file cpdir/cp_$count_out
    ((count_in++))
  done
}

getfiles $1

情景2 :

现有要求:

给定目录名称,建立给定名称(如AAA)的目录,并在该目录下建立 指定数目的具有一定命名规则(如cp_[NUM])的目录

从给定的目录(BBB)中获取1000个目录,每100个目录存储在AAA/cp_[num] 目录下:

 

创建目录:

#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
#  ensure this .sh file is changed mod
#  source is which dir you want to operate

function getfiles(){
    # 建立指定目录
    `mkdir cp_$1`
    for ((i=1; i<=12; i ++))
    do
        #循环建立目录,i<=12,12指得是需要建立目录的数目
        `mkdir cp_$1/cp_$i`
    done
}

getfiles $1

移动数据:

#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
#  ensure this .sh file is changed mod
#  source is which dir you want to operate
#  
count_in=0
count_out=1

function getfiles(){
  for file in `ls $1`
  do
    # 每100个复制一次
    if [ $count_in -eq 100 ]
    then
      # 复制100个之后,更换目录
      ((count_out++))
      count_in=0
    fi
    mv $1/$file cpdir/cp_$count_out
    ((count_in++))
  done
}

getfiles $1

情景3:

当前目录下目录命名错误,需要批量重命名的。文件在当前目录下运行: bash changename.sh ./ NEW_NAME

#!/bin/bash
# useage
# ./thisfilename.sh source
# NOTE:
#
#  ensure this .sh file is changed mod
#  source is which dir you want to operate
#  

function getfiles(){
  for file in `ls $1`
  do
    mv $file $2_$file
    #echo $file
  done
}

getfiles $1 $2

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用 Python 的 `os` 模块来读取目录下的多个子目录以及它们包含的文件,然后将它们的内容写入到一个新的文件中。 下面是一个简单的示例: ```python import os root_dir = '/path/to/root/directory' output_file = 'output.txt' with open(output_file, 'w') as f: for dirpath, dirnames, filenames in os.walk(root_dir): for filename in filenames: file_path = os.path.join(dirpath, filename) with open(file_path, 'r') as file: content = file.read() f.write(content) ``` 上面的代码会遍历 `root_dir` 目录下的所有子目录,读取它们包含的所有文件,然后将它们的内容写入到名为 `output_file` 的新文件中。 ### 回答2: 可以使用Python的os和shutil模块来读取目录下多个子目录下的文件,并将其写入一个新的文件中。 首先,使用os模块的walk函数遍历目录下的所有子目录文件。对于每一个子目录,使用os模块的listdir函数获取子目录下的所有文件名,并将其与子目录路径拼接得到完整的文件路径。 然后,可以使用open函数创建一个新的文件,写入该文件需要包含的内容。在遍历文件的过程中,逐个打开待写入的文件,读取其中的内容,然后使用文件句柄的write方法将内容写入新的文件中。 最后,记得在处理完所有文件后,调用新文件的close方法来关闭文件。 下面是一个示例代码: ```python import os import shutil # 新文件的路径和文件名 output_file = "new_file.txt" # 遍历目录下的子目录文件 for root, dirs, files in os.walk("目录路径"): for file in files: # 获取文件的完整路径 file_path = os.path.join(root, file) try: # 打开待写入文件 with open(file_path, "r") as f: # 读取文件内容 content = f.read() # 打开新文件 with open(output_file, "a") as new_file: # 写入文件内容 new_file.write(content) except IOError: print("无法打开文件:", file_path) # 关闭新文件 new_file.close() ``` 在上述代码中,需要将"目录路径"替换为你要读取的目录的路径。 这段代码会遍历目录下的所有子目录文件,并将每个文件的内容逐个写入新文件中。如果有很多文件需要处理,可以分批读取和写入,以免内存资源不足。另外,要确保新文件已经存在或者在代码中进行文件的创建。 希望以上信息能对你有所帮助! ### 回答3: 在Python中,要读取目录下多个子目录下的文件,并将其写入一个新的文件中,可以使用os模块与shutil模块来完成。下面是一个简单的代码示例: ```python import os import shutil def read_and_write_files(directory, output_file): with open(output_file, 'w') as f_out: for root, dirs, files in os.walk(directory): for filename in files: file_path = os.path.join(root, filename) with open(file_path, 'r') as f_in: content = f_in.read() f_out.write(content) f_out.write('\n') # 调用函数进行操作 directory = '目录路径' # 需要读取的目录路径 output_file = '输出文件路径' # 新文件路径 read_and_write_files(directory, output_file) ``` 在以上代码中,`read_and_write_files`函数是核心函数,它使用os模块的`walk`函数来遍历目录下的所有文件。然后通过打开每个文件,将其内容读取后追加写入到指定的新文件中。最终得到一个包含目录下所有文件内容的新文件。 需要注意的是,`directory`变量需要替换成目标目录的路径,`output_file`变量需要替换成输出文件的路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值