Python 에서 파일을 어떻게 이동합니까

내가 생각하고 있지만, 파이썬 꽂으십시오 'os' 인터페이스입니다 파일을 이동할 수 있는 방법을 찾을 수 없습니다. 어떻게 할까요? '에서' $ mv 에 있다.

>>> source_files = '/PATH/TO/FOLDER/*'
>>> destination_folder = 'PATH/TO/FOLDER'
>>> # equivalent of $ mv source_files destination_folder
질문에 대한 의견 (2)
해결책

['오드리나미 ()'] [1], ['슈틸드모브 ()'] [2] 또는 ['오스트리플레이스 ()'] [3]

같은 문법을 사용하는 모든

import os
import shutil

os.rename("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
shutil.move("path/to/current/file.foo", "path/to/new/destination/for/file.foo")
os.replace("path/to/current/file.foo", "path/to/new/destination/for/file.foo")

포함시키십시오 합니다 모두 및대상 인수만 소스 파일 이름 ('피레스포'). 물론 변경할 경우, 해당 파일은 renamed 이동했습니다. 또한 처음 두 가지 사례 디렉터리입니다 만들어지는 새 파일이 있어야 합니다. Windows 에서 그 이름을 가진 파일이 존재하지 않아야 합니다 호스트였든 idfsysobject. 오스트리플레이스 () '가 아니라' 높아질 것이라고 할 수 있는 파일을 자동으로 복귀시킴 에셋은.

그동안 명시된 것처럼 다른 대답이 ',' 댓글 '대부분의 경우' 오드리나미 슈틸드모브 호출할 수 있습니다. 그러나, 그 대신 다른 디스크 대상 켜있을 소스보다 복사한 다음 소스 파일 삭제.

[1]: http://docs.python.org/library/os.html # 오드리나미 [2]: http://docs.python.org/library/shutil.html # 슈틸드모브 [3]: https://docs.python.org/library/os.html # 오스트리플레이스

해설 (8)

비록 '오드리나미 ()' 와 '슈틸드모브 ()' 는 모두 유닉스 파일 이름 변경, 가장 가까운 슈틸드모브 () 는 'mv 명령을 명령'. 다른 점은 오드리나미 () ',' t # 39 doesn& 경우 다른 디스크에 동시에 작동합니까 소스와 대상이 슈틸드모브 () ',' t care doesn& # 39 어떤 디스크 파일 오른길로.

해설 (3)

또는 슈틸드모브 임포트하십시오 모듈에서는 중 하나를 오드리나미 합니다.

  • 문자가 없는 데 필요한 모든 파일 이동.

우리는 폴더란 dell. / opt 라는 파일을 소스 하나로 와섬.t스트리 불렀으매 / 멋져

in /opt/awesome
○ → ls
source
○ → ls source
awesome.txt

python 
>>> source = '/opt/awesome/source'
>>> destination = '/opt/awesome/destination'
>>> import os
>>> os.rename(source, destination)
>>> os.listdir('/opt/awesome')
['destination']

실제로 칼리오피 우린 오스트리스타디르 확인하기 위해 폴더 이름이 바뀌었다.

39 의, 대상 here& 슈틸 숨길까요 다시 소스로.

>>> import shutil
>>> shutil.move(destination, source)
>>> os.listdir('/opt/awesome/source')
['awesome.txt']

이번엔 제가 점검됩니다 소스 파일 폴더 안에 있는 내가 만든 와섬.t스트 확인할 수 있습니다. 그리고 그 속에는:)

이제 우리는 다른 폴더와 파일 소스에서 대상 및 다시.

해설 (2)

'후' & # 39 파트리브 파이썬 3.4 사용할 수도 있습니다, s 클래스 '길' 로 파일 이동.

그레나미 (path/to/new/destination/for/file.foo&quot ";) '' 패스 (path/to/current/file.foo&quot ";)

https://docs.python.org/3.4/library/pathlib.html # 파트리브룩스파스트라나미

해설 (1)

이게 I& # 39 를 사용하여, m 이 순간.

import os, shutil
path = "/volume1/Users/Transfer/"
moveto = "/volume1/Users/Drive_Transfer/"
files = os.listdir(path)
files.sort()
for f in files:
    src = path+f
    dst = moveto+f
    shutil.move(src,dst)

이제 전기능. ᄋ "이번 도움이 됩니다.

편집:

39 에 i&, ve 켜졌음 소스 및대상 디렉터리입니다 userdic. 받아들이는 함수, 이 경우, t # 39 대상 폴더에서는 doesn& 존재하며, 파일을 이동합니다. Src 필터링합니다 도 허용합니다 이동할 경우에만 이미지 파일, 예를 들어 '& # 39,, 그럼 패턴을 사용하여 *.jpg& # 39 의 디렉토리에는 이동하는', 기본적으로 모든 것을

import os, shutil, pathlib, fnmatch

def move_dir(src: str, dst: str, pattern: str = '*'):
    if not os.path.isdir(dst):
        pathlib.Path(dst).mkdir(parents=True, exist_ok=True)
    for f in fnmatch.filter(os.listdir(src), pattern):
        shutil.move(os.path.join(src, f), os.path.join(dst, f))
해설 (1)

이 질문에 대한 답이 수락됨 맞지 않는 파일 이름 바꾸기 때문에 하나의 파일로 이동할 수 있지만, 많은 파일 압축을 디렉터리입니다. 이를 위해 '가' 슈틸드모브 일을 할 수 없게 될 수 있기 때문에 '는' 오드리나미 명시성 파일 이름 (언급된 바와 같이 주석에 대한) 대상 있어야 합니다.

해설 (1)

설명됨 여기서요 오토메이티드 따라 또 다른 옵션을 사용하여 하위 ','.

이 같은 일이.

subprocess.call("mv %s %s" % (source_files, destination_folder), shell=True)

39, s 와 con& 알고 pro& 궁금하네요 # 39 에 비해 '이 방법의 슈틸'. 내 경우 하위 '이후' 나는 이미 사용하고, 다른 이유로 나는 스틱 it 를 통해 사용할 수 있도록 경향이 보인다.

Is it 시스템 종속적입니다 아마?

해설 (0)

이 솔루션을 통해 '쉘로 않는 "사용' mv '.

import subprocess

source      = 'pathToCurrent/file.foo'
destination = 'pathToNew/file.foo'

p = subprocess.Popen(['mv', source, destination], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
res = p.communicate()[0].decode('utf-8').strip()

if p.returncode:
    print 'ERROR: ' + res
해설 (0)
  import os,shutil

  current_path = "" ## source path

  new_path = "" ## destination path

  os.chdir(current_path)

  for files in os.listdir():

        os.rename(files, new_path+'{}'.format(f))
        shutil.move(files, new_path+'{}'.format(f)) ## to move files from 

서로 다른 디스크 emailxtender. C: - &gt. D:

해설 (1)