Python: использовать команду копирования файла - как это работает
Видео по теме: (September 2024).
Это руководство по Python показывает, как вы можете использовать команду копирования файла в Python для копирования файла. Используя простой пример, вы можете легко сделать это самостоятельно.
Используйте команду копирования файла в Python
С помощью команды copy file в Python файлы можно легко скопировать:
- Добавьте команду «from shutil import copyfile» (без кавычек) в начало вашего кода, чтобы впоследствии вы могли использовать эту команду.
- Синтаксис команды «copyfile (src, dst)». Так что просто введите значения, которые вам нужны для вашей программы.
- Примером этого может быть: "copyfile ('/ src / dir / file.txt', '/dst/dir/file_with_new_name.txt')".
- Как видите, вы можете не только скопировать ваш файл, но и изменить имя файла в целевой директории.
- Совет: Вы также можете использовать «/ dst / dir /» вместо «/dst/dir/Datei_mit_neuem_Namen.txt» в команде. Это копирует файл с оригинальным именем файла и сохраняет его в целевом каталоге.
Python: копировать файл - другие варианты
Чтобы скопировать файл в Python, мы ввели метод shutil в предыдущем абзаце. Однако вы также можете скопировать файл без модуля shutil:
- Прежде всего, вы должны добавить команду «import os» в начало вашего кода.
- В Windows теперь вы можете скопировать файл с помощью команды "os.system ('copy D: /test.txt F: /')".
- В Linux вы должны использовать команду "os.system ('cp /home/user/test.txt / tmp /')".
- В обоих случаях выполняется команда копирования. Вы также можете написать другие команды в скобках, которые затем выполняются в терминале или в CMD.
Дополнительные советы о Python в наших руководствах по ЧИПу
Подсказка: Вы можете найти дополнительные учебники по программированию на нашей странице темы языка программирования Python. Например, мы покажем вам, как создавать и использовать массивы в Python.