如何删除 Python 中带有 .git 文件夹的目录
How to delete directory with .git folder in Python
我正在尝试创建一个程序来帮助我进行备份。它通过将项目复制到备份位置来工作。在复制之前它会做一些检查。我检查该文件夹之前是否已备份,如果有我需要在复制新备份之前删除该文件夹。
当我备份没有 .git 文件夹的项目时,它工作正常。一旦项目目录有一个 .git 文件夹,它就会中断。它不会删除 .git 文件夹,这会扰乱程序的其余部分。我似乎找不到删除 .git 文件夹的方法。
我的代码:https://pastebin.com/jyGhQDB5
def EXTERNAL_BACKUP():
# Loaction of External backup, to check if the language folder exists
EXT_BACKUP_LANG_DIR = 'D:/2019/' + BACKUP_LANGUAGE
if os.path.exists(EXT_BACKUP_LANG_DIR):
# Check if project has a backup saved already
if os.path.exists(EXTERNAL_LOCATION):
print('\nFolder has already been backed up, previously. Proceeding to zip previous folder before copying new backup.')
# Check for any old zip files of the project
os.chdir(EXT_BACKUP_LANG_DIR)
for file in glob.glob(PROJECT_NAME + '.zip'):
# All zips with the name of the current project backup will be deleted
os.remove(EXT_BACKUP_LANG_DIR, file)
if os.path.exists(EXT_BACKUP_LANG_DIR + file):
os.rmdir(EXT_BACKUP_LANG_DIR + file)
# Call function to zip previous backup
MAKE_ARCHIVE(PROJECT_NAME + '--OLD', EXTERNAL_LOCATION)
# Print to console when the zipping has finished
print(PROJECT_NAME +
' has been zipped. Proceeding to delete non-zipped folder.')
# Delete the non-zipped folder before copying new backup
for the_file in os.listdir(EXTERNAL_LOCATION):
file_path = os.path.join(EXTERNAL_LOCATION, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(e)
print('Non-zipped folder has been deleted. Proceeding to backup.')
shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
print('Successfully backed up.\n')
else:
# Copy project folder as normal
print('\nBackup to external has begun.')
shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
print('Backup to the external has been completed.')
elif not os.path.exists(EXT_BACKUP_LANG_DIR):
# Backup language does not exist, so create the fodler before performing backup
os.mkdir(BACKUP_LANGUAGE)
print('\n' + BACKUP_LANGUAGE + ' not found. Created a new folder called '
+ BACKUP_LANGUAGE + '. Proceeding to backup files to external HDD.')
shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
print('Backup to the external has been completed.')
return
一段时间以来,我一直试图在 Google 上寻找答案。这个问题的答案没有用。 How to delete directory containing .git in python
这是我想出的。尝试一下,希望它会起作用。它假定您在 Windows.
注:on_rm_error方法来自this post.
import os
import stat
import shutil
from subprocess import call
dir = r'C:\Users\...\...\...'
def on_rm_error(func, path, exc_info):
#from:
os.chmod(path, stat.S_IWRITE)
os.unlink(path)
for i in os.listdir(dir):
if i.endswith('git'):
tmp = os.path.join(dir, i)
# We want to unhide the .git folder before unlinking it.
while True:
call(['attrib', '-H', tmp])
break
shutil.rmtree(tmp, onerror=on_rm_error)
我正在尝试创建一个程序来帮助我进行备份。它通过将项目复制到备份位置来工作。在复制之前它会做一些检查。我检查该文件夹之前是否已备份,如果有我需要在复制新备份之前删除该文件夹。
当我备份没有 .git 文件夹的项目时,它工作正常。一旦项目目录有一个 .git 文件夹,它就会中断。它不会删除 .git 文件夹,这会扰乱程序的其余部分。我似乎找不到删除 .git 文件夹的方法。
我的代码:https://pastebin.com/jyGhQDB5
def EXTERNAL_BACKUP():
# Loaction of External backup, to check if the language folder exists
EXT_BACKUP_LANG_DIR = 'D:/2019/' + BACKUP_LANGUAGE
if os.path.exists(EXT_BACKUP_LANG_DIR):
# Check if project has a backup saved already
if os.path.exists(EXTERNAL_LOCATION):
print('\nFolder has already been backed up, previously. Proceeding to zip previous folder before copying new backup.')
# Check for any old zip files of the project
os.chdir(EXT_BACKUP_LANG_DIR)
for file in glob.glob(PROJECT_NAME + '.zip'):
# All zips with the name of the current project backup will be deleted
os.remove(EXT_BACKUP_LANG_DIR, file)
if os.path.exists(EXT_BACKUP_LANG_DIR + file):
os.rmdir(EXT_BACKUP_LANG_DIR + file)
# Call function to zip previous backup
MAKE_ARCHIVE(PROJECT_NAME + '--OLD', EXTERNAL_LOCATION)
# Print to console when the zipping has finished
print(PROJECT_NAME +
' has been zipped. Proceeding to delete non-zipped folder.')
# Delete the non-zipped folder before copying new backup
for the_file in os.listdir(EXTERNAL_LOCATION):
file_path = os.path.join(EXTERNAL_LOCATION, the_file)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print(e)
print('Non-zipped folder has been deleted. Proceeding to backup.')
shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
print('Successfully backed up.\n')
else:
# Copy project folder as normal
print('\nBackup to external has begun.')
shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
print('Backup to the external has been completed.')
elif not os.path.exists(EXT_BACKUP_LANG_DIR):
# Backup language does not exist, so create the fodler before performing backup
os.mkdir(BACKUP_LANGUAGE)
print('\n' + BACKUP_LANGUAGE + ' not found. Created a new folder called '
+ BACKUP_LANGUAGE + '. Proceeding to backup files to external HDD.')
shutil.copytree(SOURCE_DIRECTORY, EXTERNAL_LOCATION)
print('Backup to the external has been completed.')
return
一段时间以来,我一直试图在 Google 上寻找答案。这个问题的答案没有用。 How to delete directory containing .git in python
这是我想出的。尝试一下,希望它会起作用。它假定您在 Windows.
注:on_rm_error方法来自this post.
import os
import stat
import shutil
from subprocess import call
dir = r'C:\Users\...\...\...'
def on_rm_error(func, path, exc_info):
#from:
os.chmod(path, stat.S_IWRITE)
os.unlink(path)
for i in os.listdir(dir):
if i.endswith('git'):
tmp = os.path.join(dir, i)
# We want to unhide the .git folder before unlinking it.
while True:
call(['attrib', '-H', tmp])
break
shutil.rmtree(tmp, onerror=on_rm_error)