python 鎵撳紑鏂囦欢澶
浠ヤ笅鏄绉嶄笉鍚岀殑瀹炵幇鏂规硶鎵撳紑鏂囦欢澶癸細

鏂规硶1: 浣跨敤os妯″潡鎵撳紑鏂囦欢澶

import os
 
folder_path = '璺緞/鍒/鏂囦欢澶'
os.chdir(folder_path)  # 鍒囨崲鍒版枃浠跺す鐩綍
鏂规硶2: 浣跨敤pathlib妯″潡鎵撳紑鏂囦欢澶

from pathlib import Path
 
folder_path = Path('璺緞/鍒/鏂囦欢澶')
os.chdir(folder_path)  # 鍒囨崲鍒版枃浠跺す鐩綍
鏂规硶3: 浣跨敤os妯″潡鍒楀嚭鏂囦欢澶逛腑鐨勬枃浠跺拰瀛愭枃浠跺す

import os
 
folder_path = '璺緞/鍒/鏂囦欢澶'
files = os.listdir(folder_path)  # 鍒楀嚭鏂囦欢澶逛腑鐨勬枃浠跺拰瀛愭枃浠跺す
for file in files:
    print(file)
鏂规硶4: 浣跨敤glob妯″潡鑾峰彇鏂囦欢澶逛腑鎵鏈夋枃浠剁殑璺緞

import glob
 
folder_path = '璺緞/鍒/鏂囦欢澶'
files = glob.glob(folder_path + '/*')  # 鑾峰彇鏂囦欢澶逛腑鎵鏈夋枃浠剁殑璺緞
for file in files:
    print(file)
鏂规硶5: 浣跨敤os妯″潡鍒ゆ柇璺緞鏄惁涓烘枃浠跺す

import os
 
folder_path = '璺緞/鍒/鏂囦欢澶'
if os.path.isdir(folder_path):  # 鍒ゆ柇璺緞鏄惁涓烘枃浠跺す
    print("璇ヨ矾寰勬槸涓涓枃浠跺す")
else:
    print("璇ヨ矾寰勪笉鏄竴涓枃浠跺す")
璇锋敞鎰忥紝涓婅堪鐨勬柟娉曟瘡涓兘鏈変笉鍚岀殑鐗圭偣鍜岄傜敤鍦烘櫙锛屽叿浣撲娇鐢ㄥ摢绉嶆柟娉曞彇鍐充簬浣犵殑闇姹傚拰鎯呭喌銆

浠ヤ笂灏辨槸python 鎵撳紑鏂囦欢澶圭殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒



鏈枃URL锛http://www.odweb.cn/news_show.html?id=280