Python を使用してディレクトリ内の複数のファイルの名前を変更する
ディレクトリ内の複数のファイルの名前を手動で変更するのは、面倒な作業になる可能性があります。ただし、Python には、このプロセスをより効率的かつ正確に自動化するためのオプションがいくつか用意されています。
アプローチ:
os.path.split の使用
import os folder = 'dir' files = os.listdir(folder) for file in files: # Split the filename and extension filename, ext = os.path.splitext(file) # Modify the filename modified_filename = filename.split('_')[1] # Combine the modified filename with the extension new_file = modified_filename + ext # Rename the file os.rename(os.path.join(folder, file), os.path.join(folder, new_file))
文字列操作の使用:
import os folder = 'dir' files = os.listdir(folder) for file in files: if file.startswith('CHEESE_'): # Remove 'CHEESE_' from the filename new_file = file[7:] # Rename the file os.rename(os.path.join(folder, file), os.path.join(folder, new_file))
os.rename の使用
import os folder = 'dir' files = os.listdir(folder) for file in files: if file.startswith('CHEESE_'): # Get the new filename new_file = file[7:] # Use os.rename() to change the filename os.rename(os.path.join(folder, file), os.path.join(folder, new_file))
すべてこれらのアプローチは、「CHEESE_」プレフィックスを削除し、元の拡張子を保持して、ディレクトリ内のファイルの名前を変更するという望ましい結果を実現します。
以上がPython を使用してディレクトリ内の複数のファイルの名前を変更する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

pythonusesahybridmodelofcompilation andtertation:1)thepythoninterpretercompilessourcodeodeplatform-indopent bytecode.2)thepythonvirtualmachine(pvm)thenexecuteTesthisbytecode、balancingeaseoputhswithporformance。

pythonisbothintersedand compiled.1)it'scompiledtobytecode forportabalityacrossplatforms.2)bytecodeisthenは解釈され、開発を許可します。

loopsareideal whenyouwhenyouknumberofiterationsinadvance、foreleloopsarebetterforsituationsは、loopsaremoreedilaConditionismetを使用します

henthenumber ofiterationsisknown advanceの場合、dopendonacondition.1)forloopsareideal foriterating over for -for -for -saredaverseversives likelistorarrays.2)whileopsaresupasiable forsaresutable forscenarioswheretheloopcontinupcontinuspificcond

pythonisnotpurelyLepted; itusesahybridapproachofbytecodecodecodecodecodecodedruntimerttation.1)pythoncompilessourcodeintobytecode、whodythepythonvirtualmachine(pvm).2)

ToconcatenateListsinpythothesheElements、使用:1)Operatortokeepduplicates、2)asettoremoveduplicates、or3)listcomplunting for controloverduplicates、各メトドハスディフェルフェルフェントパフォーマンスアンドソーダーインプリテーション。

pythonisantertedlanguage、useaseofuseandflexibility-butfactingporformantationationsincriticalapplications.1)解釈されたlikepythonexecuteline-by-lineを解釈します

Useforloopswhenthenumberofiterationsisknowninadvance、andwhiloopswheniterationsdependonacondition.1)forloopsareidealforsecenceslikelistoranges.2)


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

WebStorm Mac版
便利なJavaScript開発ツール

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール
