누구든지 PC의 한 폴더에 여러 하위 폴더의 특정 확장자를 가진 파일을 수집하고 이동시키는 부드러운 파이썬 스크립트가 있습니까?python 스크립트가 여러 개의 하위 폴더에있는 .mp3 파일을 모두 가져와 PC의 한 폴더로 이동 하시겠습니까?
0
A
답변
0
:
먼저 .mp3
로 끝나는 모든 파일을 찾을 수 :
import os
filepath = []
for path, _, files in os.walk('./'):
#You might want to exclude some directories or limit search files found to directories:
#exclude paths that have 'Program Files' folder and only include all paths that have 'Downloads',
#ie derp/Downloads/a/ or Downloads/durp/ included, anything with 'Program Files' excluded
if ('Program Files' not in path) and ('Downloads' in path):
#in case of windows system replace \ with/
filepath.extend([os.path.join(path, file).replace('\\', '/') for file in files
if str(file).endswith('.mp3')])
전체 파일 경로가있는 파일 목록을 가지고 있으므로 새 위치로 이동하십시오 :
for file in filepath:
os.rename(filepath, 'new/folder' + os.path.split(filpath)[1])
0
os.chdir("D:\DOWNLOAD") # Change file direction first
for file in glob.glob("*.mp3"): # search for the *.mp3 extension
time.sleep(1)
shutil.move("path/to/current/file", "path/to/new/destination/for/file")
이 같은, 당신은 모든 하위 방향을 얻기 위해 운영 체제 기능을 통해 산책을 추가해야
대신 글로브를 사용+0
glob를 사용해야합니다. – ExtraMt
0
방법에 대한
from dkfileutils.path import Path
[mp3.move('/destination/dir') for mp3 in Path('/source/dir').glob('**/*.mp3')]
(전체 공개 : 나는 dkfileutils의 저자 해요)
먼저 자신을 시도하세요! 나는 파이썬에 익숙하며 당신은 10 줄의 코드에서 그렇게 할 수있다. 나는 너를 믿는다! :) – monamona
정말 파이썬으로 필요합니까? 간단한 명령 줄만으로도 충분합니다. – vmonteco
os.walk()와 os.rename()을 파일링이 포함 된 튜플과 함께 사용하십시오. 필자가 항상 제공하는 팁 중 하나는 os.rename()을 사용하지 않고 예상 한 것처럼 보이는지 확인하기 위해 print (originpath, endpath)를 인쇄하는 것입니다. 그 일이 진행되면서 os rename으로 인쇄를 바꿉니다. 행운을 빕니다! –