How to move a file?

后端 未结 9 1779
夕颜
夕颜 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: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}")
    

提交回复
热议问题