How to move a file?

后端 未结 9 1780
夕颜
夕颜 2020-11-22 01:07

I looked into the Python os interface, but was unable to locate a method to move a file. How would I do the equivalent of $ mv ... in Python?

&g         


        
相关标签:
9条回答
  • 2020-11-22 01:14

    Although os.rename() and shutil.move() will both rename files, the command that is closest to the Unix mv command is shutil.move(). The difference is that os.rename() doesn't work if the source and destination are on different disks, while shutil.move() doesn't care what disk the files are on.

    0 讨论(0)
  • 2020-11-22 01:14

    The accepted answer is not the right one, because the question is not about renaming a file into a file, but moving many files into a directory. shutil.move will do the work, but for this purpose os.rename is useless (as stated on comments) because destination must have an explicit file name.

    0 讨论(0)
  • 2020-11-22 01:20
      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 
    

    different disk ex. C: --> D:

    0 讨论(0)
  • 2020-11-22 01:21

    This is solution, which does not enables shell using mv.

    import subprocess
    
    source      = 'pathToCurrent/file.foo'
    destination = 'pathToNew/file.foo'
    
    p = subprocess.Popen(['mv', source, destination], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    output = p.communicate()[0].decode('utf-8').strip()
    
    if p.returncode:
        print(f"E:{output}")
    
    0 讨论(0)
  • 2020-11-22 01:22

    After Python 3.4, you can also use pathlib's class Path to move file.

    from pathlib import Path
    
    Path("path/to/current/file.foo").rename("path/to/new/destination/for/file.foo")
    

    https://docs.python.org/3.4/library/pathlib.html#pathlib.Path.rename

    0 讨论(0)
  • 2020-11-22 01:28

    os.rename(), shutil.move(), or os.replace()

    All employ the same syntax:

    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")
    

    Note that you must include the file name (file.foo) in both the source and destination arguments. If it is changed, the file will be renamed as well as moved.

    Note also that in the first two cases the directory in which the new file is being created must already exist. On Windows, a file with that name must not exist or an exception will be raised, but os.replace() will silently replace a file even in that occurrence.

    As has been noted in comments on other answers, shutil.move simply calls os.rename in most cases. However, if the destination is on a different disk than the source, it will instead copy and then delete the source file.

    0 讨论(0)
提交回复
热议问题