48 lines
No EOL
1.4 KiB
Python
48 lines
No EOL
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Script pour ajouter un commentaire en début de fichier CSV afin d'éviter
|
|
qu'il soit exécuté directement comme un script Python.
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
|
|
def fix_csv_file(csv_file_path):
|
|
"""
|
|
Ajoute un commentaire en début de fichier CSV pour éviter l'exécution directe.
|
|
"""
|
|
if not os.path.exists(csv_file_path):
|
|
print(f"Erreur: Le fichier {csv_file_path} n'existe pas.")
|
|
return False
|
|
|
|
# Lire le contenu actuel du fichier
|
|
with open(csv_file_path, 'r') as f:
|
|
content = f.read()
|
|
|
|
# Vérifier si le fichier commence déjà par un commentaire
|
|
if content.startswith('#'):
|
|
print(f"Le fichier {csv_file_path} est déjà protégé.")
|
|
return True
|
|
|
|
# Ajouter le commentaire au début du fichier
|
|
with open(csv_file_path, 'w') as f:
|
|
f.write('# Ce fichier est un CSV et ne doit pas être exécuté directement avec Python.\n')
|
|
f.write(content)
|
|
|
|
print(f"Le fichier {csv_file_path} a été protégé contre l'exécution directe.")
|
|
return True
|
|
|
|
def main():
|
|
"""
|
|
Fonction principale.
|
|
"""
|
|
# Utiliser le fichier spécifié en argument ou par défaut 'suivi_livre.csv'
|
|
if len(sys.argv) > 1:
|
|
csv_file = sys.argv[1]
|
|
else:
|
|
csv_file = 'suivi_livre.csv'
|
|
|
|
fix_csv_file(csv_file)
|
|
|
|
if __name__ == "__main__":
|
|
main() |