目录指南中的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\\
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值