Процесс не может получить доступ к файлу так как этот файл занят другим процессом python
Перейти к содержимому

Процесс не может получить доступ к файлу так как этот файл занят другим процессом python

  • автор:

Процесс не может получить доступ к файлу: файл занят другим процессом

Хотел создать что-то по типу builder на python, у меня была такая задумка: создать текстовый документ в него вписать код после чего переиминовать файл txt в py и ввести в cmd с помощью subprocess команду cd и директорию файла, после чего командой pyinstaller откомпилировать файл в exe, но когда я запустил builder возникла вот такая ошибка:

вот код отвечающий за создание билда:

file.close() перед перемещением

Не закрыт файл после записи. Сделайте:

Дизайн сайта / логотип © 2023 Stack Exchange Inc; пользовательские материалы лицензированы в соответствии с CC BY-SA . rev 2023.6.8.43486

Нажимая «Принять все файлы cookie» вы соглашаетесь, что Stack Exchange может хранить файлы cookie на вашем устройстве и раскрывать информацию в соответствии с нашей Политикой в отношении файлов cookie.

pip installation error #60

image

Installation without compiling works well.
Working on this issue.

The text was updated successfully, but these errors were encountered:

We can solve the problem by capturing console output, decode it and then print normal decoded text.

The thing is that I didn’t find 100% sure way to determine which encoding we should apply. For example: I know that in my situation it is cp866 (standard Russian codepage for console) which is returned by chcp Windows command. Nevertheless, locale.getpreferredencoding() returns cp1251 (standard Russian encoding for GUI), sys.stdout.encoding returns utf-8 . And it’s only Russian case.

Using sys.setdefaultencoding() is not a good variant (article).

So, I’ve come to the conclusion that we can just suppress the third-party output (from Visual Studio, CMake, etc.) and have no problems.

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

Just to confirm, Python is the only program running on my computer. What is causing this problem and how do I fix it?

martineau's user avatar

5 Answers 5

Your process is the one that has the file open (via im still existing). You need to close it first before deleting it.

I don’t know if PIL supports with contexts, but if it did:

This will make sure to delete im (and close the file) before you get to os.remove .

If it doesn’t you might want to check out Pillow, since PIL development is pretty much dead.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *