Pythonで同じ名前のファイルの名前を変更しながら、ディレクトリ内のすべてのファイルを新しいディレクトリにコピーします

wes.e:

ソースディレクトリ内のすべてのcsvファイルをコピーしようとしています。そのサブフォルダーは新しい「メガ」フォルダーにあります。最終結果は、ソースディレクトリ内で見つかったcsvファイル以外の何も含まないフォルダーになります。

私が直面している問題は、csvファイル名の一部が同じであるということです。したがって、ファイルをコピーすると、同じ名前のファイルが上書きされます。上書きするのではなく、名前を変更できるようにしたい。ファイルの名前を変更する形式の例は次のとおりです。

  • あいうえお
  • abcd_1
  • abcd_2など...

このスレッドを見つけましたが、答えがうまくいきませんでした。

私のコードは次のとおりです(提供されたリンクに基づく):

movdir = r"Source Directory"
basedir = r"Destination Folder"
# Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
    for filename in files:
        # I use absolute path, case you want to move several dirs.
        old_name = os.path.join(os.path.abspath(root), filename)

        # Separate base from extension
        base, extension = os.path.splitext(filename)

        # Initial new name
        new_name = os.path.join(basedir, base, filename)

        # If folder basedir/base does not exist... You don't want to create it?
        if not os.path.exists(os.path.join(basedir, base)):
            print(os.path.join(basedir,base), "not found") 
            continue    # Next filename
        elif not os.path.exists(new_name):  # folder exists, file does not
            shutil.copy(old_name, new_name)
        else:  # folder exists, file exists as well
            ii = 1
            while True:
                new_name = os.path.join(basedir,base, base + "_" + str(ii) + extension)
                if not os.path.exists(new_name):
                   shutil.copy(old_name, new_name)
                   print("Copied", old_name, "as", new_name)
                   break 
                ii += 1

このコードを実行すると、ソースディレクトリ内のすべてのcsvファイルが "見つかりません"と表示され、ファイルはまったくコピーされません。

これに関するヘルプや情報をいただければ幸いです。

Yehdhih ANNA:

次の変更を試してください。

movedir = r"source"
basedir = r"destination"
# Walk through all files in the directory that contains the files to copy
for root, dirs, files in os.walk(movdir):
    for filename in files:
        # I use absolute path, case you want to move several dirs.
        old_name = os.path.join(os.path.abspath(root), filename)

        file_path,file_bare_name = os.path.split(filename) # this is were ur code didn't work as u use base as the bare file name and the relative path to source ambiguously.
        base, extension = os.path.splitext(file_bare_name)
        file_relative_path_to_source = root[len(movedir)+1:] #removing the old dir name from the relative path 
        
        if extension=='.csv': # taking only csv files
            # Initial new name
            new_name = os.path.join(basedir, file_bare_name)
            if not os.path.exists(new_name):  # file dosn't exist
                shutil.copy(old_name, new_name)
            else:  # copies being renamed
                ii = 1
                while True:
                    new_name = os.path.join(basedir, file_bare_name + "_" + str(ii) + extension)
                    if not os.path.exists(new_name):
                        shutil.copy(old_name, new_name)
                        print("Copied", old_name, "as", new_name)
                        break 
                    ii += 1

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ