-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModule-OS.py
More file actions
128 lines (89 loc) · 4.39 KB
/
Module-OS.py
File metadata and controls
128 lines (89 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
########## OS MODULE ##########
# os module enables you to:
# get information about the operating system
# manage processes
# operate on I/O streams using file descriptors
### Getting information about the operating system
# os module provides a function called uname which returns an object containing the following attributes:
# systemname = stores the name of the operating system
# nodename = stores the machine name on the network
# release = stores the operating system release
# version = stores the operating system version
# machine = stores the hardware identifier
## os.name()
import os
print(os.name)
# nt
### Creating directories in Python
## os.mkdir()
# crée un dossier
import os
os.mkdir("my_first_directory")
print(os.listdir())
# ['.git', '.vscode', 'ascii.pdf', 'bac-à-sable.py', 'Books', 'Boolean&Comparatifs.py', 'Break-Continue.py', 'data.txt', 'data_engineer_skills.txt',
# 'Dictionnaries.py', 'file.bin', 'file.bin.bck', 'Files.py', 'For.py', 'Functions-BUILT-IN.py', 'Functions.py', 'Generators-Iterators.py', 'If-Else.py',
# 'Lambda.py', 'List.py', 'Logical-Bitwise.py', 'Methods-DICTIONARIES.py', 'Methods-FILE.py', 'Methods-LIST.py', 'Methods-NUMBER.py', 'Methods-SET.py',
# 'Methods-STRING.py', 'Methods-TUPLE.py', 'Module-Test', 'Module.py', 'Modules-BUILT-IN.py', 'my_first_directory', 'newtext.txt', 'Numbers.py', 'Numpy-ARRAYS.py',
# 'Numpy-FUNCTIONS.py', 'Numpy.py', 'OOP.py', 'os_module.py', 'Package.py', 'Pip.py', 'Print.py', 'processus.xlsx', 'projects', 'Python-key-words.py',
# 'Python_error_tree.mmd', 'Score Report.pdf', 'Set.py', 'Strings.py', 'text.txt', 'Try&Except.py', 'Tuples.py', 'Variables.py', 'While.py']
## os.listdir(projects)
# crée une list des sous-réperoires et files du répertoire courant ou celui indiqué comme argument
# **Note :** L'ordre peut varier selon le système d'exploitation (pas forcément alphabétique)
print(os.listdir("projects"))
# ['AP-wifi.svg', 'caesar_cipher.py', 'calcul BMI.py', 'calcul heure fin de reunion.py', 'calcul investissement intéret.py',
# 'calcul_moyenne_classe.py', 'classer&compter.py', 'configuration réseau.py', 'copy_paste.py', 'créer un hostname.py',
# 'créer_video_image_fixe.py', 'Derminer si on a un triangle.py', 'Find prime numbers.py', 'Fuel-consumption-conversion.py',
# 'gérer_anniversaires_amis.py', 'IBAN_validator', 'inventaire_produits.py', 'investment_calculator.py', 'Leap or Common year.py',
# 'LIST_vs_GENERATOR_for_file_anlalysis', 'lyrics into chart.py', 'Numbers of days in any year-month pair.py',
# 'number_processor.py', 'python_error-branch.py', 'Somme produit factoriel.py', 'Suite de Fibonacci.py', 'Tax_calculator.py',
# 'Temperature_convertor.py', 'TicTacToe.py', 'time-calculator.py', 'volume.py']
## os.makedirs("dir1/"dir2"/../"dirX")
# permet de créer des dossiers et sous-dossiers de façon récursive
import os
os.makedirs("my_first_directory/my_second_directory")
# créée 2 dossiers: my_first_directory et my_second_directory
## os.chdir("dir1")
# permet de changer de directory
import os
os.chdir("my_first_directory")
print(os.listdir())
# ['my_second_directory']
## os.getcwd()
# = pwd = permet de savoir dans quel répertoire courrant on se situe, CHEMIN COMPLET
import os
os.makedirs("my_first_directory/my_second_directory")
os.chdir("my_first_directory")
print(os.getcwd())
# C:\PythonLearning\my_first_directory
os.chdir("my_second_directory")
print(os.getcwd())
# C:\PythonLearning\my_first_directory\my_second_directory
## os.rmdir("dirX")
# permet de supprimer un dossier vide
import os
os.makedirs("my_first_directory/my_second_directory")
os.rmdir("my_first_directory")
# si repertoire en contient d'autres:
# OSError: [WinError 145] Le répertoire n’est pas vide: 'my_first_directory'
## os.removedirs("dir1/"dir2"/../"dirX")
# permet de supprimer une chaine de dosssiers
import os
os.removedirs("my_first_directory/my_second_directory")
## os.path.isfile()
# Vérifie si le chemin correspond à un fichier existant
import os
file_path = input("Entrez le chemin du fichier : ")
# example.txt
if os.path.isfile(file_path):
print("Le fichier existe.")
else:
print("Le fichier n'existe pas.")
# Le fichier existe.
## os.path.join()
import os
import sys
zip_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'packages', 'extrapack.zip'))
sys.path.append(zip_path)
import os
print(os.path.join('usr', 'bin', 'spam'))
# usr\\bin\\spam