Python3 では、rename() メソッドと listdir() メソッドを使用して複数のファイルの名前を変更できます。どちらのメソッドも os モジュールの一部であり、非常に便利です。次の記事では、rename() メソッドと listdir() メソッドについて説明し、rename() メソッドと listdir() メソッドを使用して複数のファイルの名前を変更する方法を紹介します。
os.rename() メソッド
os.rename() メソッドが使用されますファイルまたはディレクトリの名前を変更します。このメソッドには戻り値がありません。 [ビデオチュートリアルの推奨:Python3 チュートリアル]
基本構文:
os.rename(src, dst)● パラメーター src: 必要なファイルのソース アドレスを示します。 be rename# パラメータ dst: リネーム後の対象ファイル名を示します。
説明: エラーレポート
## 該当するファイルまたはディレクトリがリネームされた場合、パラメータ src が存在しない場合、エラーが保存されます;#●パラメータ dst に対応するファイルまたはディレクトリがすでに存在する場合、エラーが報告されます。
os.listdir() メソッド os.listdir() メソッドは、指定されたフォルダーに含まれるファイルまたはフォルダーを返すために使用されます。名前のリスト。
注:os.listdir() メソッドによって返されるリストはアルファベット順であり、フォルダー内にある場合でも '.' と '..' は含まれません。 。
基本構文:os.listdir(path)
パラメータ パス: リストする必要があるディレクトリ パスを示します
複数の名前を変更する方法ファイル ? demo という名前のフォルダーがあり、その中に n 枚の写真が含まれています:
ここで、必要なのは、それらの名前を変更することです。 hostel1、hostel2、...などのような秩序ある方法。これを手動で行うのは面倒な作業ですが、os モジュールの rename() メソッドと listdir() メソッドを使用して実現できます。
次のコード例は、rename() メソッドと listdir() メソッドが複数のファイルの名前を変更する方法を示しています。
コード例:# 重命名目录或文件夹中的多个文件 # 导入os模块 import os #重命名多个文件 def main(): i = 1 # listdir()函数的作用是:列出指定目录的所有内容 for filename in os.listdir("demo"): dst ="PHP" + str(i) + ".jpg" src ='demo'+ filename dst ='demo'+ dst # rename()函数的作用是:重命名所有文件 os.rename(src, dst) i += 1 # Driver Code if __name__ == '__main__': # 调用main()函数 main()
出力結果:
以上です。記事の内容全体が皆さんの研究に役立つことを願っています。さらにエキサイティングなコンテンツについては、PHP 中国語 Web サイトの関連チュートリアルのコラムに注目してください。 ! !
以上がPython で複数のファイルの名前を変更する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

Dreamweaver Mac版
ビジュアル Web 開発ツール
