【问题标题】:fins and move files with path name with python3fins 并使用 python3 移动带有路径名的文件
【发布时间】:2020-06-22 21:28:22
【问题描述】:

我正在尝试从我的 perl 脚本中重新创建 python 脚本,以查找所有具有通用名称 model1_r.pdb 的文件,然后他们将其移动到一个新文件夹,其新文件夹的名称与之前的现有文件夹相同。 这是我写的python代码;

import os, shutil, glob
# This program will list all pdb file named (model1_r.pdb) in a given directory.
# inpout: directory name 
# output: pdb files

source = input("enter filepath: ")

# build the command to look for model1_r.pdb
for file_path in glob.glob(os.path.join(source, '*model1_r.pdb')):
        new_dir = file_path.rsplit('/', 1)[1]
        find = os.system("find -type f -name \"model1_r.pdb\" ")
        print(filepath)
        destination = "/gs/gsfs0/users/kdhusia/dir_all_pdb/"
        #Move the files to new folder based with filepath folder name
        shutil.move(find, ios.path.join(destination, os.path.basename(filepath)))

这是我的 perl 脚本,它运行良好,但速度很慢

use strict;
use warnings;
use Data::Dumper;
use File::Basename;

# This program will list all pdb file named (model1_r.pdb) in a given directory.
# inpout: directory name 
# output: pdb files

my $dir = $ARGV[0] || undef;
die "a directory is required\n" unless $dir;

chomp $dir;

# build the command to look for model1_r.pdb
my $cmd = "find $dir -type f -name \"model1_r.pdb\" "; 
#my $cmd = "find $dir -type f -name \"*.pdb\" "; 
print $cmd, "\n";

my @output = `$cmd`;

my $DEST = "/gs/gsfs0/users/kdhusia/dcomplex_all";
for my $line (@output) {
   # remove CR from $line
   chomp $line;
   #print $line;
   # get pair-name from line
   my @pairs = split("\/", $line);
   #print Dumper(\@pairs);

   my $new_name = $pairs[2].".pdb";
   #print "new_name = $new_name\n";

   # copy model1_r.pdb to dinal destination
   # change the name of pdb to "pair-name.pdb"
   my $cmd = "cp $line $DEST/$new_name"; 
   # my $cmd = "./dcomplex $pdbfile A B "
   print "$cmd\n";

   # do the copy
   system($cmd);
}

我使用堆栈上的先前讨论进行了转换(Python: Moving files to folder based on filenames)

我在存储文件路径的过程中遗漏了一些东西。任何 cmets 将不胜感激。

【问题讨论】:

  • 基于你的假设,python 版本会比 perl 运行得更快?

标签: python perl hpc sys


【解决方案1】:

这是python3的倒数代码:

import os
import shutil
import glob
import argparse

# This program will list all pdb file named (model1_r.pdb) in a given directory.
# inpout: directory name 
# output: pdb files

parser = argparse.ArgumentParser()
parser.add_argument("file", help="filename is required", type=str)
args = parser.parse_args()

print (args.file)

dest = "/gs/gsfs0/users/.../dir_all_pdb/"
# build the command to look for model1_r.pdb
#for f in os.path.listdir(source):
for root, dirs, files in os.walk(args.file):
    for file in files:
        if file.endswith("l1_r.pdb"):
            #print (file)
            print ("moving ...")
            print(os.path.join(root, file)) # printing full pathname
            # extratc the name ofthe pair. position 6 identifies tha name of the pair 
            pair_name =  os.path.join(root, file).split("/")
            name = pair_name[6]

            # buildong final destination ful path name 
            f_name = dest + name+'.pdb'
            print ("to ...", f_name)           

            src = os.path.join(root, file)
            shutil.copy( src, f_name)

这需要目录路径来查找文件名,然后将其与找到文件的文件夹名称一起存储到目标文件夹中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    相关资源
    最近更新 更多