Zanurkuj w Pythonie/Praca z katalogami: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Sasek (dyskusja | edycje)
tłumaczenie
Piotr (dyskusja | edycje)
częściowe tłumaczenie
Linia 111:
>>> glob.glob('c:\\music\\*\\*.mp3') #(4)
 
# Jak wcześniej powiedzieliśmy, <tt>os.listdir</tt> po prostu z podanej ścieżki do folderu, zwraca wszystkie pliki i foldery, które się w nim znajdują.
# As you saw earlier, os.listdir simply takes a directory path and lists all files and directories in that directory.
# Z innej strony, moduł <tt>glob</tt> na podstawie podanego wyrażenia (ang. ''wildcard''), zwraca pełne ścieżki wszystkich plików, które spełniają te wyrażenie. Tutaj wyrażenie jest ścieżką do katalogu plus "*.mp3", który będzie dopasowywał wszystkie pliki <tt class="lang-none">.mp3</tt>. Dodajmy, że każdy element zwracanej listy jest już pełną ścieżką do pliku.
# The glob module, on the other hand, takes a wildcard and returns the full path of all files and directories matching the wildcard. Here the wildcard is a directory path plus "*.mp3", which will match all .mp3 files. Note that each element of the returned list already includes the full path of the file.
# If you want to find all the files in a specific directory that start with "s" and end with ".mp3", you can do that too.
# Now consider this scenario: you have a music directory, with several subdirectories within it, with .mp3 files within each subdirectory. You can get a list of all of those with a single call to glob, by using two wildcards at once. One wildcard is the "*.mp3" (to match .mp3 files), and one wildcard is within the directory path itself, to match any subdirectory within c:\music. That's a crazy amount of power packed into one deceptively simple-looking function!