惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
腾讯CDC
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
L
LangChain Blog
Vercel News
Vercel News
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
Microsoft Azure Blog
Microsoft Azure Blog
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园_首页
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
IT之家
IT之家
A
About on SuperTechFans
H
Help Net Security

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Search and Replace in Multiple Files in Vim/Neovim Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN
How to Use shutil.move() on Windows and Linux
2018-11-15 · via jdhao's digital space

Today when I try to move a file using shutil.move() on my Windows machine, I encounter an error message:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

In this post, I will write about what I have learned from this error.

How to move files correctly on Windows#

On Windows, before moving a file, you must close it. Or, you will see the above error message.

Suppose that we want to move images in a child directory images/ to another child directory small_image/ if the width of an image is below a threshold. On Windows system, the correct way to do it is like the following:

from glob import glob
from PIL import Image

all_images = glob("images/*.jpg")

for i, im_path in enumerate(all_images):
    im = Image.open(im_path)
    width = im.width
    # we must close the image before moving it to another directory
    im.close()

    if width < 15:
        shutil.move(im_path, 'small_images/')

On Linux, you are not required to close the file before moving it, i.e., you can move a file even if it is opened by another process.

How to move a file if a file with the same name already exists in the destination directory?#

On both Linux and Windows, when you try to move a file using shutil.move(src, dst) with dst set as a directory path, you will encounter the following error message if a file with the same name already exists under dst:

shutil.Error: Destination path ‘./test.txt’ already exists

The solution is to use the full file path in dst, i.e., a complete path to the new file. If a file with the same name exists under the destination folder, it will be silently replaced. If that behaviour is not what you want, you may consider renaming the file under the new directory.

References#