如何在Python中移动文件

本文翻译自:How to move a file in Python

I looked into the Python os interface, but was unable to locate a method to move a file. 我查看了Python的os界面,但无法找到移动文件的方法。 How would I do the equivalent of $ mv ... in Python? 我将如何在Python中做相当于$ mv ...工作?

>>> source_files = '/PATH/TO/FOLDER/*'
>>> destination_folder = 'PATH/TO/FOLDER'
>>> # equivalent of $ mv source_files destination_folder

#1楼

参考:https://stackoom.com/question/bAN6/如何在Python中移动文件


#2楼

For either the os.rename or shutil.move you will need to import the module. 对于os.rename或shutil.move,您将需要导入模块。 No * character is necessary to get all the files moved. 要移动所有文件,无需*字符。

We have a folder at /opt/awesome called source with one file named awesome.txt. 我们在/ opt / awesome处有一个名为source的文件夹,其中有一个名为awesome.txt的文件。

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']

We used os.listdir to see that the folder name in fact changed. 我们使用os.listdir来查看文件夹名称实际上已更改。 Here's the shutil moving the destination back to source. 这是将目标移回源的途径。

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

This time I checked inside the source folder to be sure the awesome.txt file I created exists. 这次,我在源文件夹中进行了检查,以确保我创建的awesome.txt文件存在。 It is there :) 在那儿:)

Now we have moved a folder and its files from a source to a destination and back again. 现在,我们已经将文件夹及其文件从源移动到了目的地,然后又移回了。


#3楼

Although os.rename() and shutil.move() will both rename files, the command that is closest to the Unix mv command is shutil.move() . 尽管os.rename()shutil.move()都将重命名文件,但是最接近Unix mv命令的命令是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. 不同的是, os.rename()不工作,如果源和目标是不同的磁盘上,而shutil.move()不关心这些文件在什么盘。


#4楼

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. shutil.move将完成工作,但为此目的, os.rename是无用的(如注释中所述),因为目标必须具有显式文件名。


#5楼

This is what I'm using at the moment: 这是我目前正在使用的:

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)

Now fully functional. 现在功能齐全。 Hope this helps you. 希望这对您有所帮助。

Edit: 编辑:

I've turned this into a function, that accepts a source and destination directory, making the destination folder if it doesn't exist, and moves the files. 我已经将其转换为一个函数,该函数接受源目录和目标目录,并创建目标文件夹(如果不存在)并移动文件。 Also allows for filtering of the src files, for example if you only want to move images, then you use the pattern '*.jpg' , by default, it moves everything in the directory 还允许过滤src文件,例如,如果只想移动图像,则使用模式'*.jpg' ,默认情况下,它将移动目录中的所有内容

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

#6楼

  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: C:-> D:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值