目錄指南中的Python列表文件-listdir VS system(“ ls”)通過示例進行解釋

🔹歡迎 (🔹 Welcome)

If you want to learn how these functions work behind the scenes and how you can use their full power, then this article is for you.

如果您想了解這些功能在后臺如何工作以及如何充分利用它們的功能,那么本文適合您。

We will start by diving into concepts that are essential to work with listdir and system:

我們將首先探討對于使用listdirsystem必不可少的概念:

  • The built-in Python os module and how to import it.

    內置的Python os模塊以及如何導入它。

  • The concepts of "directory" and "current working directory".

    “目錄”和“當前工作目錄”的概念。
  • How to check and change your current working directory.

    如何檢查和更改您當前的工作目錄。
  • The difference between an absolute path and a relative path.

    絕對路徑和相對路徑之間的差異。

Then, we will dive into the functions themselves:

然后,我們將深入研究這些函數:

  • How to work with the listdir function and when to use it.

    如何使用listdir函數以及何時使用它。

  • How to work with the system("ls") function and when to use it.

    如何使用system("ls")函數以及何時使用它。

  • Examples of both of them and how they work behind the scenes.

    兩者的示例以及它們在后臺的工作方式。

Let's begin! ?

讓我們開始! ?

OS操作系統模塊 (🔸 The OS Module)

The two functions that we will discuss: listdir() and system() belong to the os module. This module includes functions that are used to interact with your operating system, performing actions like:

我們將討論的兩個函數: listdir()system()屬于os模塊。 該模塊包括用于與操作系統交互的功能,它們執行以下操作:

  • Making a new directory.

    制作一個新目錄。
  • Renaming an existing directory.

    重命名現有目錄。
  • Removing a directory.

    刪除目錄。
  • Displaying the path to your current working directory.

    顯示當前工作目錄的路徑。
  • Much more!

    多得多!

💡 Tips:

💡 提示:

  • A directory is what we commonly know as a "folder", where we usually store related files and/or other directories, creating a hierarchy of directories within directories that are called subdirectories. An example of a directory is your "Documents" folder.

    目錄是我們通常所說的“文件夾”,我們通常在其中存儲相關文件和/或其他目錄,從而在稱為子目錄的目錄內創建目錄的層次結構。 目錄的一個示例是“文檔”文件夾。

  • A module is a file that contains related Python code.

    模塊是包含相關Python代碼的文件。

如何導入操作系統模塊 (How to Import the OS Module)

To use the os module in your script, you need to "import" it. Importing a module means gaining access to all the functions and variables that are stored within the module. We import a module when we want to use its code in our script.

要在腳本中使用os模塊,您需要“導入”它。 導入模塊意味著可以訪問模塊中存儲的所有功能和變量。 當我們想在腳本中使用其代碼時,我們將導入一個模塊。

To import the os module, you simply need to include this line at the top of your Python script or run this line in the interactive shell:

要導入os模塊,您只需要在Python腳本的頂部包含以下行或在交互式shell中運行此行:

import os

This will give you access to all the functions defined in the os module.

這將使您可以訪問os模塊中定義的所有功能。

💡 Tip: this module was already installed when you installed Python 3, so you will be able to use it immediately.

💡 提示:安裝Python 3時已經安裝了此模塊,因此您可以立即使用它。

To be able to use the functions from the os module, you will need to add the prefix os. before the name of the function that you want to call, like this:

為了能夠使用os模塊中的功能,您將需要添加前綴os. 要調用的函數名稱之前,如下所示:

os.<function>(<params>)

For example:

例如:

os.mkdir("New Folder")

如何導入單個功能 (How to Import Individual Functions)

If you are only going to work with one or two functions from the module, you can import them individually using this syntax:

如果只打算使用模塊中的一個或兩個功能,則可以使用以下語法分別導入它們:

from <module> import <function1>, <function2>, ...

For example:

例如:

from os import listdir, system

In this case, you can call the functions in your script as you normally would, without adding the os. prefix, like this:

在這種情況下,您可以像往常一樣在腳本中調用函數, 而無需添加os. 前綴,像這樣:

<function>(<params>)

For example:

例如:

mkdir("New Folder")

🔹當前工作目錄 (🔹 Current Working Directory)

Now let's see a very important concept that you need to know before you start working with listdir and system. Your current working directory, as the name implies, is the directory (folder) where you are currently working.

現在,讓我們看看在開始使用listdirsystem之前需要了解的一個非常重要的概念。 顧名思義,您當前的工作目錄就是您當前工作的目錄(文件夾)。

You can check your current working directory with this function from the os module:

您可以從os模塊中使用此功能檢查當前的工作目錄:

os.getcwd()

This will show you the path to your current working directory.

這將向您顯示當前工作目錄的路徑。

💡 Tip: cwd means "current working directory."

💡 提示: cwd意思是“當前工作目錄”。

從交互式外殼 (From the Interactive Shell)

If I run this command in the interactive shell (Windows), I see this:

如果在交互式外殼程序(Windows)中運行此命令,則會看到以下信息:

>>> os.getcwd()
'C:\\Users\\estef\\AppData\\Local\\Programs\\Python\\Python38-32'

This is the full path to my current working directory:

這是我當前工作目錄的完整路徑:

'C:\\Users\\estef\\AppData\\Local\\Programs\\Python\\Python38-32'

從腳本 (From a Script)

If I run this command from a script, like this:

如果我從腳本運行此命令,如下所示:

import os
print(os.getcwd())

I see:

我懂了:

C:\Users\estef\Documents\freeCodeCamp\freeCodeCamp News\listdir vs system

The full path to the script (its location in the system, in the hierarchy of directories).

腳本的完整路徑(其在系統中的位置,在目錄層次結構中)。

💡 Tip: If you run a script (a Python file), your current working directory is the directory where the script is currently in.

💡 提示:如果運行腳本(Python文件),則當前的工作目錄是腳本當前所在的目錄。

如何更改當前工作目錄 (How to Change your Current Working Directory)

You can change your current working directory with this command from the os module:

您可以通過os模塊中的以下命令更改當前工作目錄:

os.chdir(<path>)

You will need to specify the path to the new working directory, passing it as an argument, formatted as a string. It can be either an absolute path or a relative path.

您將需要指定新工作目錄的路徑,并將其作為參數傳遞,格式為字符串。 它可以是絕對路徑,也可以是相對路徑。

💡 Tip:

💡 提示:

  • An absolute path specifies all the sequence of directories that you need to go through to reach your target directory. This path starts from the root directory of your system.

    絕對路徑指定到達目標目錄所需的所有目錄順序。 此路徑從系統的根目錄開始。

For example:

例如:

>>> import os
>>> os.chdir(r"C:\Users\estef\Documents\FreeCodeCamp\freeCodeCamp News\9 - listdir vs system")# Checking current working directory:
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system'

Notice that I added an r before the absolute path to convert the string into a raw string. If you use \ and you don't add the r, you will get an error because the \ symbol will be treated as a special character.

請注意,我在絕對路徑之前添加了r ,以將字符串轉換為原始字符串。 如果您使用\而未添加r ,則會出現錯誤,因為\符號將被視為特殊字符。

Alternatively, you could replace the backslashes ?\ with forward slashes / in the path:

或者,您可以在路徑中將反斜杠\替換為正斜杠/

>>> os.chdir("C:/Users/estef/Documents/FreeCodeCamp/freeCodeCamp News/9 - listdir vs system")# Checking current working directory
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system'
  • A relative path specifies the path that you want to follow to find the target directory, but now the path starts from your current working directory. It's shorter and simpler than the absolute path.

    相對路徑指定了要查找目標目錄所要遵循的路徑,但是現在該路徑從當前工作目錄開始。 它比絕對路徑更短,更簡單。

For example, if your current working directory contains a subdirectory (folder) Directory 1, you can move to this directory using a relative path (imagine it as a folder within another folder, and we are going deeper and deeper into the hierarchy), like this:

例如,如果您當前的工作目錄包含一個子目錄(文件夾) Directory 1 ,則可以使用相對路徑(將其想象為另一個文件夾中的一個文件夾,并將其深入層次結構)移至該目錄。這個:

>>> import os
>>> os.chdir(".\Directory 1")# Check current working directory
>>> os.getcwd()
'C:\\Users\\estef\\Documents\\FreeCodeCamp\\freeCodeCamp News\\9 - listdir vs system\\Directory 1'

💡 Tip: The dot (.) at the beginning of the relative path .\Directory 1 represents the current working directory. A double dot ( ..) is used to move up the hierarchy, to the "parent" directory.

💡 提示:相對路徑.\Directory 1開頭的點( . )表示當前工作目錄。 雙點( .. )用于將層次結構向上移動到“父”目錄。

Now that you have all the background knowledge that you will need to truly understand how listdir and system work, let's see them in detail.

現在,您已經具備了真正了解listdirsystem如何工作所需的所有背景知識,下面讓我們對其進行詳細介紹。

🔸Listdir (🔸 Listdir)

We will start with the listdir function. Let's reveal its mysteries. 🔮

我們將從listdir函數開始。 讓我們揭示它的奧秘。 🔮

目的和返回值 (Purpose and Return Value)

According to the Python Documentation, the purpose of this function is to:

根據Python文檔 ,此功能的目的是:

Return a list containing the names of the entries in the directory given by path.

返回包含path指定的目錄中條目名稱的列表。

Basically, this function returns a list with the names of all files and directories that are currently found within a particular directory that you specify when you call the function.

基本上,此函數返回一個列表,其中包含在調用該函數時指定的特定目錄中當前找到的所有文件和目錄的名稱。

💡 Tip: The list will not have a specific order, even if you usually sort the elements alphabetically.

💡 提示:即使您通常按字母順序對元素進行排序,列表也沒有特定的順序。

語法和參數 (Syntax and Parameter)

To call listdir, will need to use this syntax:

調用listdir ,將需要使用以下語法:

The parameter path is precisely that, the absolute or relative path to the directory that you want to visualize. In Python 3.2 and above, this parameter is optional. By default, the path will lead to your current working directory if you don't pass an argument.

參數path正是要顯示的目錄的絕對或相對路徑。 在Python 3.2及更高版本中,此參數是可選的。 默認情況下,如果不傳遞參數,該路徑將導致當前工作目錄。

Remember that you must import the os module before calling this function.

請記住,在調用此函數之前,必須導入os模塊。

💡 Tip: If you use this import statement from os import listdir to import the function individually, you can omit the os. prefix, like this:

提示:如果您使用from os import listdir import語句單獨導入函數,則可以省略os. 前綴,像這樣:

用例和優勢 (Use Cases and Advantages)

The function listdir is very helpful because it works on any operating system where Python runs, so if Python is installed on your device, this function will work correctly.

listdir函數非常有用,因為它可在運行Python的任何操作系統上運行,因此,如果您的設備上安裝了Python,則此函數將正常運行。

Now let's talk a little bit about its return value. Since it returns a list, we can store this list in a variable and work with it in our program.

現在讓我們談談它的返回值。 由于它返回一個列表,因此我們可以將該列表存儲在變量中并在程序中使用它。

For example, let's say that we want to do something with all the files from a given directory, such as converting images to black and white or modifying their content. We could do it using a for loop, like this:

例如,假設我們要處理給定目錄中的所有文件,例如將圖像轉換為黑白或修改其內容。 我們可以使用for循環來做到這一點,就像這樣:

images = os.listdir(<path>)for image in images:# Do something to the image

Of course, you would need to define what happens within the loop, but this is an example of what you could do with this function.

當然,您需要定義循環中發生的事情,但這是您可以使用此函數進行操作的一個示例。

This is awesome, right?

太棒了吧?

But having files and directories in the same list can be a little bit problematic if we want to work with a for loop, right? We would need to add a conditional to check the type of each element. How can we make a list that only contains file names (no directories) or vice versa?

但是,如果我們要使用for循環,將文件和目錄放在同一列表中可能會有些問題,對吧? 我們需要添加一個條件來檢查每個元素的類型。 我們如何制作僅包含文件名(無目錄)的列表,反之亦然?

Let's see! ?

讓我們來看看! ?

僅包含文件 (Only Include Files)

If you want to "filter" the list returned by os.listdir() to include only files (no directories) you can use this line of code:

如果要“過濾” os.listdir()返回的列表以僅包含文件 (不包含目錄),則可以使用以下代碼行:

list(filter(os.path.isfile, os.listdir(<path>)))

💡 Tip: You can customize the <path> argument or omit it to use your current working directory.

💡 提示:您可以自定義<path>參數或忽略它以使用當前工作目錄。

Let's see an example with my current working directory (I'm using Windows):

讓我們來看一個當前工作目錄的示例(我正在使用Windows):

My directory (folder) has:

我的目錄(文件夾)有:

  • Two subdirectories (folders within the main folder)

    兩個子目錄(主文件夾中的文件夾)
  • One PowerPoint file

    一個PowerPoint文件
  • One image (.png file)

    一幅圖片(.png文件)
  • One Python script

    一個Python腳本

If I call the listdir function from the script.py file and print the list returned:

如果我從script.py文件調用listdir函數并打印返回的列表:

print(os.listdir())

This is the output:

這是輸出:

['Diagrams.ppt', 'Directory 1', 'Directory 2', 'listdir vs system.png', 'script.py']

You can see that all files and directories from my current working directory were included.

您可以看到包含了當前工作目錄中的所有文件和目錄。

To filter the list to only contain files, we can use this statement:

要過濾列表以僅包含文件,我們可以使用以下語句:

print(list(filter(os.path.isfile, os.listdir())))

Now the output is:

現在的輸出是:

['Diagrams.ppt', 'listdir vs system.png', 'script.py']

Notice how the directories were "filtered", exactly what we wanted.

注意目錄是如何被“過濾”的,正是我們想要的。

僅包括目錄 (Only Include Directories)

Similarly, if you want to "filter" the list to include only directories, you can use this line of code:

同樣,如果您要“過濾”列表以僅包含目錄 ,則可以使用以下代碼行:

list(filter(os.path.isdir, os.listdir(<path>)))

Now the output is:

現在的輸出是:

['Directory 1', 'Directory 2']

Exactly what we wanted. But how does this statement work behind the scenes? Let's see.

正是我們想要的。 但是,這種說法在幕后如何運作? 讓我們來看看。

filter()在幕后的工作方式 (How filter() Works Behind the Scenes)

The filter function is called using this syntax:

使用以下語法調用filter函數:

filter(<function>, <list>)

It basically "filters" the elements of the second argument (the list) based on the truth value returned by calling the function passed as the first argument (os.path.isfile() or os.path.isdir() in their respective commands):

基本上,它基于通過調用作為第一個參數傳遞的函數( os.path.isfile()os.path.isdir()在各自的命令中os.path.isdir()返回的真值而“過濾”第二個參數(列表)的元素。 ):

print(list(filter(os.path.isfile, os.listdir())))
list(filter(os.path.isdir, os.listdir()))

These two functions:

這兩個功能:

os.path.isfile(<path>)os.path.isdir(<path>)

Return True if the argument is a file or a directory, respectively. ?

如果參數分別是文件或目錄,則返回True

Based on these truth values, the elements of the list will be included (or not) in the final "filtered" list. The elements of the list returned by os.listdir() are passed one by one to these functions to check if they are files (or directories, respectively).

基于這些真值,列表的元素將包含(或不包含)在最終的“過濾”列表中。 os.listdir()返回的列表元素將一一傳遞給這些函數,以檢查它們是否分別是文件(或目錄)。

For example: If we have this line of code:

例如:如果我們有以下代碼行:

filter(os.path.isfile, os.listdir())))

And os.listdir() returns this list:

os.listdir()返回以下列表:

['Diagrams.ppt', 'Directory 1', 'Directory 2', 'script.py']

The first element of the list ('Diagrams.ppt') is passed as argument to os.path.isfile() to check if it's a file :

列表的第一個元素( 'Diagrams.ppt' )作為參數傳遞給os.path.isfile()以檢查它是否為文件:

os.path.isfile('Diagrams.ppt') # True

The function call returns True, so it's a file and it's included in the list.

該函數調用返回True ,因此它是一個文件,并且包含在列表中。

But if the element is a directory:

但是,如果元素是目錄:

os.path.isfile('Directory 1') # False

The function call returns False, so it's not included in the list. This process continues for every element in the list until the new list only contains file names.

該函數調用返回False ,因此它不包含在列表中。 對于列表中的每個元素,此過程將繼續進行,直到新列表僅包含文件名為止。

Then, since filter() returns an iterable, we make a list from this iterable using list():

然后,由于filter()返回一個可迭代對象,因此我們使用list()從該可迭代對象中創建一個列表:

list(filter(os.path.isfile, os.listdir()))

And we print it since we are working with a Python file (script):

由于我們正在使用Python文件(腳本),因此我們將其打印出來:

print(list(filter(os.path.isfile, os.listdir())))

💡 Tip: You can visually identify if an element of the list represents a file or a directory by seeing if it has an extension (type) after its name. For example: Diagrams.ppt has a .ppt extension that tells you that it's a PowerPoint file but a directory doesn't have an extension, like 'Directory 1'.

提示:您可以通過查看列表中元素的名稱后是否具有擴展名(類型)來直觀地識別列表中的元素代表文件還是目錄。 例如: Diagrams.ppt具有.ppt擴展名,它告訴您它是PowerPoint文件,但目錄沒有擴展名,如'Directory 1'

🔹系統(“ ls”) (🔹 System("ls"))

Now that you know how to work with listdir, let's see how the system() function works behind the scenes and how you can use it. 🔮

現在,您知道如何使用listdir ,讓我們看看system()函數如何在后臺工作以及如何使用它。 🔮

目的 (Purpose )

According to the Python Documentation, the purpose of the system() function is to:

根據Python文檔 , system()函數的目的是:

Execute the command (a string) in a subshell
在子shell中執行命令(字符串)

Basically, this function takes a command (as a string) and executes it.

基本上,此函數接受命令(作為字符串)并執行它。

In this case, the command that we are passing is 'ls' , a Unix command used in Linux to display the content of a directory as standard output.

在這種情況下,我們傳遞的命令是'ls' ,這是Linux中使用的Unix命令,用于將目錄的內容顯示為標準輸出。

Unlike listdir, the system() function will not return a list if we pass the 'ls' command, it will only display the list of files and directories as standard output. Therefore, you should use it if you only want to visualize the list without actually working with it in your program.

listdir不同,如果我們通過'ls'命令, system()函數將不會返回列表 ,它只會將文件和目錄的列表顯示為標準輸出。 因此,如果您只想可視化列表而不在程序中實際使用它,則應使用它。

語法和參數 (Syntax and Parameter)

To call this function, you will need to use this syntax:

要調用此函數,您將需要使用以下語法:

Its only argument is the command that you want to execute formatted as a string (surrounded by double quotes or single quotes).

它唯一的參數是要執行的命令,格式為字符串(用雙引號或單引號引起來)。

Particularly, the ls command lets you see the content of your current working directory.

特別是,通過ls命令,您可以查看當前工作目錄的內容。

For example: if this is my working directory (three Python files and one subdirectory):

例如:如果這是我的工作目錄(三個Python文件和一個子目錄):

And I call the system() function, like this:

然后調用system()函數,如下所示:

>>> import os
>>> os.system("ls")

This is the output:

這是輸出:

'Directory 1'  'file 1.py'  'file 2.py'   main.py
0

We can see the standard output in the console (the list of files and directories):

我們可以在控制臺中看到標準輸出(文件和目錄的列表):

'Directory 1'  'file 1.py'  'file 2.py'   main.py

and the return value:

和返回值:

0

💡 Note: For these examples of the system() function, I'm working with an online command line tool called Repl.it since my computer has Windows installed and the command ls is not recognized by the default command prompt.

💡 注意:對于這些system()函數的示例,由于我的計算機已安裝Windows,并且默認命令提示符無法識別ls命令,因此我正在使用名為Repl.it的在線命令行工具。

局限性 (Limitations)

One of the main limitation of this function is that the command passed as argument has to be recognized by the operating system or environment that you are working with.

此功能的主要限制之一是作為參數傳遞的命令必須由您使用的操作系統或環境識別。

For example, the ls command will not be recognized in Windows by default in the command prompt. You will see this error if you try to run it:

例如,默認情況下,在命令提示符下,Windows無法識別ls命令。 如果嘗試運行它,將會看到此錯誤:

'ls' is not recognized as an internal or external command, operable program or batch file.
無法將“ ls”識別為內部或外部命令,可操作程序或批處理文件。

A similar command in Windows would be the 'dir' command:

Windows中類似的命令是'dir'命令:

os.system('dir')

💡 Tip: There are alternative ways to run the ls command on Windows, such as using terminal programs that recognize Unix commands, but by default Windows does not recognize the 'ls' command.

💡提示:還有其他方法可以在Windows上運行ls命令,例如使用識別Unix命令的終端程序,但是默認情況下Windows無法識別'ls'命令。

返回值 (Return Value)

According to the Python documentation:

根據Python文檔 :

On Unix, the return value is the exit status of the process encoded in the format specified for wait().

在Unix上,返回值是以為wait()指定的格式編碼的進程的退出狀態。

and...

和...

On Windows, the return value is that returned by the system shell after running command.

在Windows上,返回值是在運行command之后由系統外殼返回的值。

💡 ?Tip: Note that this function does not return a list. It simply displays the list as standard output, so you can't store it in a variable like you did with listdir.

💡 提示:請注意,此功能不會返回列表。 它只是將列表顯示為標準輸出,因此您不能像使用listdir一樣將其存儲在變量中。

ls命令的變體 (Variations of the ls command)

A key feature of os.system('ls') is that it has many helpful and interesting options to customize how present the output. Let's see some of them.

os.system('ls')的關鍵功能是它具有許多有用且有趣的選項,可自定義輸出的顯示方式。 讓我們來看一些。

Option 1: We can show more information about files and directories such as their size, location, and modification date and time using the command ls -l.

選項1:我們可以使用命令ls -l顯示有關文件和目錄的更多信息,例如文件和目錄的大小,位置以及修改日期和時間。

>>> import os
>>> os.system('ls -l')
total 12
drwxr-xr-x 1 runner runner  0 Apr  3 18:23 'Directory 1'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 1.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 2.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38  main.py
0

Option 2: To be able to visually recognize directories faster, we can use ls -F, which adds a forward slash / to the end of their names (see 'Directory 1/' below).

選項2:為了能夠在視覺上更快地識別目錄,我們可以使用ls -F ,在其名稱的末尾添加一個斜杠/ (請參見下面的'Directory 1/' )。

>>> import os
>>> os.system('ls -F')
'Directory 1'/  'file 1.py'  'file 2.py'   main.py
0

Option 3: To sort the files by size, we can use the command ls -lS.

選項3:要按大小對文件排序,我們可以使用命令ls -lS

>>> import os
>>> os.system('ls -lS')
total 12
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 1.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38 'file 2.py'
-rw-r--r-- 1 runner runner 11 Apr  3 18:38  main.py
drwxr-xr-x 1 runner runner  0 Apr  3 18:23 'Directory 1'
0

There are many more options for customization that can be helpful for your particular goal. Here you can find more information about the -ls command and how you can use its full power.

還有許多自定義選項可以對您的特定目標有所幫助。 在這里,您可以找到有關-ls命令以及如何使用其全部功能的更多信息 。

list listdir與system(“ ls”)的摘要 (🔸 Summary of listdir vs. system("ls"))

  • Purpose: listdir returns the list of file names and directories in the path specified (by default, the current working directory) while system("ls") only displays them as standard output.

    用途: listdir返回指定路徑(默認情況下為當前工作目錄)中文件名和目錄的列表,而system("ls")僅將它們顯示為標準輸出。

  • Operating System: listdir can be used independently of the operating system that you are working with. In contrast, system('ls') has to be executed in an operating system or environment that recognizes the 'ls' command.

    操作系統: listdir可以獨立于所使用的操作系統使用。 相反,必須在可識別'ls'命令的操作系統或環境中執行system('ls')

  • Customization: you can filter the list returned by listdir if you need to remove files or directories using the filter() function and you can pass options to customize the output of system('ls').

    自定義:如果需要使用filter()函數刪除文件或目錄,則可以過濾listdir返回的列表,并且可以傳遞選項以自定義system('ls')的輸出。

I really hope that you liked my article and found it helpful. Now you can work with these functions in your Python projects. Check out my online courses. Follow me on Twitter. 👍

我真的希望您喜歡我的文章并發現它對您有所幫助。 現在,您可以在Python項目中使用這些功能。 查看我的在線課程 。 在Twitter上關注我。 👍

翻譯自: https://www.freecodecamp.org/news/python-list-files-in-a-directory-guide-listdir-vs-system-ls-explained-with-examples/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390550.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390550.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390550.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java多線程并發學習-進階大綱

1、synchronized 的實現原理以及鎖優化&#xff1f;2、volatile 的實現原理&#xff1f;3、Java 的信號燈&#xff1f;4、synchronized 在靜態方法和普通方法的區別&#xff1f;5、怎么實現所有線程在等待某個事件的發生才會去執行&#xff1f;6、CAS&#xff1f;CAS 有什么缺陷…

大數據定律與中心極限定理_為什么中心極限定理對數據科學家很重要?

大數據定律與中心極限定理數據科學 (Data Science) The Central Limit Theorem is at the center of statistical inference what each data scientist/data analyst does every day.中心極限定理是每個數據科學家/數據分析師每天所做的統計推斷的中心。 Central Limit Theore…

useEffect語法講解

useEffect語法講解 用法 useEffect(effectFn, deps)能力 useEffect Hook 相當于 componentDidMount&#xff0c;componentDidUpdate 和 componentWillUnmount 這三個函數的組合。 可以模擬渲染后、更新后、銷毀三個動作。 案例演示 渲染后更新標題 useEffect(()>{doc…

leetcode 726. 原子的數量

給定一個化學式formula&#xff08;作為字符串&#xff09;&#xff0c;返回每種原子的數量。 原子總是以一個大寫字母開始&#xff0c;接著跟隨0個或任意個小寫字母&#xff0c;表示原子的名字。 如果數量大于 1&#xff0c;原子后會跟著數字表示原子的數量。如果數量等于 1…

web相關基礎知識1

2017-12-13 09:47:11 關于HTML 1.絕對路徑和相對路徑 相對路徑&#xff1a;相對于文件自身為參考。 &#xff08;工作中一般是使用相對路徑&#xff09; 這里我們用html文件為參考。如果說html和圖片平級&#xff0c;那直接使用src 如果說圖片在和html平級的文件夾里面&#xf…

JavaScript循環:標簽語句,繼續語句和中斷語句說明

標簽聲明 (Label Statement) The Label Statement is used with the break and continue statements and serves to identify the statement to which the break and continue statements apply. Label語句與break和continue語句一起使用&#xff0c;用于標識break和continue語…

馬約拉納費米子:推動量子計算的“天使粒子”

據《人民日報》報道&#xff0c;以華人科學家為主體的科研團隊找到了正反同體的“天使粒子”——馬約拉納費米子&#xff0c;從而結束了國際物理學界對這一神秘粒子長達80年的漫長追尋。該成果由加利福尼亞大學洛杉磯分校何慶林、王康隆課題組&#xff0c;美國斯坦福大學教授張…

leetcode 1711. 大餐計數

大餐 是指 恰好包含兩道不同餐品 的一餐&#xff0c;其美味程度之和等于 2 的冪。 你可以搭配 任意 兩道餐品做一頓大餐。 給你一個整數數組 deliciousness &#xff0c;其中 deliciousness[i] 是第 i?????????????? 道餐品的美味程度&#xff0c;返回你可以用…

您的第一個簡單的機器學習項目

This article is for those dummies like me, who’ve never tried to know what machine learning was or have left it halfway for the sole reason of being overwhelmed. Follow through every line and stay along. I promise you’d be quite acquainted with giving yo…

eclipse報Access restriction: The type 'BASE64Decoder' is not API處理方法

今天從svn更新代碼之后&#xff0c;由于代碼中使用了BASE64Encoder 更新之后報如下錯誤&#xff1a; Access restriction: The type ‘BASE64Decoder’ is not API (restriction on required library ‘D:\java\jdk1.7.0_45\jre\lib\rt.jar’) 解決其實很簡單&#xff0c;把JR…

【躍遷之路】【451天】程序員高效學習方法論探索系列(實驗階段208-2018.05.02)...

(躍遷之路)專欄 實驗說明 從2017.10.6起&#xff0c;開啟這個系列&#xff0c;目標只有一個&#xff1a;探索新的學習方法&#xff0c;實現躍遷式成長實驗期2年&#xff08;2017.10.06 - 2019.10.06&#xff09;我將以自己為實驗對象。我將開源我的學習方法&#xff0c;方法不斷…

react jest測試_如何使用React測試庫和Jest開始測試React應用

react jest測試Testing is often seen as a tedious process. Its extra code you have to write, and in some cases, to be honest, its not needed. But every developer should know at least the basics of testing. It increases confidence in the products they build,…

面試題 17.10. 主要元素

題目 數組中占比超過一半的元素稱之為主要元素。給你一個 整數 數組&#xff0c;找出其中的主要元素。若沒有&#xff0c;返回 -1 。請設計時間復雜度為 O(N) 、空間復雜度為 O(1) 的解決方案。 示例 1&#xff1a; 輸入&#xff1a;[1,2,5,9,5,9,5,5,5] 輸出&#xff1a;5 …

簡單團隊-爬取豆瓣電影T250-項目進度

本次主要講解一下我們的頁面設計及展示最終效果&#xff1a; 頁面設計主要用到的軟件是&#xff1a;html&#xff0c;css&#xff0c;js&#xff0c; 主要用的編譯器是&#xff1a;sublime&#xff0c;dreamweaver&#xff0c;eclipse&#xff0c;由于每個人使用習慣不一樣&…

鴿子為什么喜歡盤旋_如何為鴿子回避系統設置數據收集

鴿子為什么喜歡盤旋鴿子回避系統 (Pigeon Avoidance System) Disclaimer: You are reading Part 2 that describes the technical setup. Part 1 gave an overview of the Pigeon Avoidance System and Part 3 provides details about the Pigeon Recognition Model.免責聲明&a…

scrum認證費用_如何獲得專業Scrum大師的認證-快速和慢速方式

scrum認證費用A few months ago, I got the Professional Scrum Master Certification (PSM I). 幾個月前&#xff0c;我獲得了專業Scrum Master認證(PSM I)。 This is a trending certification nowadays, because most companies operate with some sort of agile methodolo…

981. 基于時間的鍵值存儲

創建一個基于時間的鍵值存儲類 TimeMap&#xff0c;它支持下面兩個操作&#xff1a; set(string key, string value, int timestamp) 存儲鍵 key、值 value&#xff0c;以及給定的時間戳 timestamp。 get(string key, int timestamp) 返回先前調用 set(key, value, timesta…

前端開發-DOM

文檔對象模型&#xff08;Document Object Model&#xff0c;DOM&#xff09;是一種用于HTML和XML文檔的編程接口。它給文檔提供了一種結構化的表示方法&#xff0c;可以改變文檔的內容和呈現方式。我們最為關心的是&#xff0c;DOM把網頁和腳本以及其他的編程語言聯系了起來。…

css 繪制三角形_解釋CSS形狀:如何使用純CSS繪制圓,三角形等

css 繪制三角形Before we start. If you want more free content but in video format. Dont miss out on my Youtube channel where I publish weekly videos on FrontEnd coding. https://www.youtube.com/user/Weibenfalk----------Are you new to web development and CSS?…