Still Using the OS Module in Python? This Alternative is Remarkably Better
还在用Python的操作系统吗?这种替代方案明显更好
Still Using the OS Module in Python? This Alternative is Remarkably Better
还在用Python的操作系统吗?这种替代方案明显更好
Python’s OS module is a nightmare for managing files and folders. You should try Pathlib.
Python的操作系统是管理文件和文件夹的噩梦。您应该尝试使用pathlib标准库模块。
File and folder management with Python’s os module is a nightmare. Yet, it’s an essential part of every data science workflow. Saving reports, reading configuration files, you name it — there’s no way around it.
用Python的操作系统进行文件和文件夹管理是一个噩梦。但它又是每个数据科学工作流程中必不可少的一部分,例如保存报告,读取配置文件。
Picture this — you spend weeks building an API around your model, and it works flawlessly, at least on your machine. Once deployed, it’s a whole different story. Your API fails in unexpected places or even won’t run, as absolute paths you’ve hardcoded simply don’t exist.
比如你花了几周时间围绕你的模型构建了一个API,它在你的电脑上运行良好。可是一旦部署,情况就完全不同了。因为硬编码的绝对路径根本不存在,API可能在意外的地方失败,甚至无法运行。
There’s a no-brainer solution. The pathlib library comes by default with Python 3.4 and above. It’s by far the most humane way to work with files, folder, and their connection in your apps. The best thing is — today you’ll learn all about it. I’m not saying that the os module serves no purpose, just that pathlib is far superior in file and folder management.
有一个简单的解决方案。Python3.4及更高版本默认提供pathlib库。这是迄今为止处理文件,文件夹,以及它们在应用程序中的连接最人性化的方式。最棒的是今天你就会学到所有的东西。我并不是说操作系统没有作用,只是说pathlib在文件和文件夹管理方面要好得多。
To start, create a new Python file and import the pathlib library:
首先,创建一个新的Python文件并导入pathlib库:
You’ll need to be inside a .py file for some functionalities to work. For example, you won’t have access to __file__ property in Jupyter Notebooks. Everything else should work perfectly.
有些功能只有在 .py 文件里才能生效,比如 __file__ 这个属性在 Jupyter Notebook 里是用不了的。不过除此之外,其他所有功能都完全没问题。
Here’s what you’ll learn today:
以下是您今天要学习的内容:
Get a path to the current Python file
获取当前Python文件的路径
Every so often, you need an absolute path to your working directory with the included Python file name. You can obtain it quickly with pathlib. Keep in mind, this command won’t work in Jupyter Notebooks, as you can’t access the __file__ property there. Skip this section if you’re a Notebook user.
获取当前脚本文件所在目录的绝对路径,有时您会需要用到(包含文件名本身)。借助 pathlib 可以快速实现。但请注意,这条命令在 Jupyter Notebook 中无法运行,因为那里访问不到 __file__ 属性。如果您是 Notebook 用户,可以直接跳过本段。
Anyhow, here’s how to get an absolute path + a file name of the Python script:
无论如何,下面是获取Python脚本的绝对路径+文件名的方法:
Here’s how it looks on my machine:
下面是它在我的机器上的运行过程:
Easy, right? Right.
很容易,对吗?是的。
There’s an easier solution if you don’t need the file name.
如果不需要文件名,有一个更简单的解决方案。
Get a path to the current working directory
获取当前工作目录的路径
This one is equivalent to executing pwd in the Unix shell. It will return a path to the directory you’re currently in, or where the running script is located.
该命令的作用相当于在 Unix Shell 中执行 pwd 命令。它会返回当前所在目录的路径,也就是正在运行的脚本所在的目录。
Here’s how to use it:
下面是它的使用方法:
And here’s what it prints on my machine:
这是它在我的机器上打印的内容:
No file name, as you can see.
没有文件名,如您所见。
But what if you need to access a file in a parent folder? That would be Desktop in my case. Let’s cover that next.
但如果你需要访问父目录里的文件呢?比如在我的电脑上,父目录就是 Desktop。接下来我们就来讲这个。
Get a first parent folder path
获取第一个父文件夹路径
This one is easy. You only need to access the parent property of a current working directory. Here’s how it’s done:
这个就简单了,只需要调用当前工作目录的 parent 属性就行了。具体做法如下:
Here’s what it prints on my machine:
下面是它在我的机器上展示的内容:
Great! But what if one parent folder isn’t enough? Let’s see what your options are.
太棒了!但是如果一个父文件夹不够呢?让我们看看你有什么选择。
Get an Nth parent folder path
获取第n个父文件夹路径
You have options. The first one is to call access the parent property multiple times like a crazy person. Here’s an example:
方法不止一种。第一种嘛,就是像疯了似的一遍遍调用 parent 属性——给你看个例子:
The easier option is to access the parents property array and index it. For instance, here’s how you’d get a path to the second parent folder path:
更简单的选项是访问parents属性数组并对其进行索引。例如,以下是获取第二个父文件夹路径的方法:
Here are the results:
以下是结果:
Array indexing starts at 0, so accessing parents[1] gets you to the second parent folder.
数组索引从0开始,因此访问父文件夹[1]将使您进入第二个父文件夹。
You now have enough knowledge to start joining paths. Let’s see how next.
您现在有足够的知识开始连接路径。让我们看看接下来如何。
Join paths
联接路径
Let’s say that a folder with sales reports is located two directories above your current location, and the report is called summer-sales.csv. Is there a way to access it with an absolute path?
假设一个带有销售报告的文件夹位于您当前位置的两个目录之上,报告名为summer-sales.csv。有没有一种方法可以用绝对路径访问它?
Of course there is.
当然。
You already know how to access the Nth parent folder path. You’ll extend that functionality by calling joinpath() and providing the report name as an argument:
您已经知道如何访问第n个父文件夹路径。您将通过调用joinpath()并提供报告名称作为参数来扩展该功能:
Here’s what it prints on my machine:
下面是它在我的机器上显示的内容:
The joinpath() function is probably the one I use the most. It’s super useful.
joinpath()函数可能是我使用最多的函数,它超级有用。
Create a directory if it doesn’t exist
如果目录不存在,则创建该目录
If I had a dollar every time something in production failed because I forgot to create a directory… It’s a pretty common mistake, and pathlib allows you to get around it without too much hassle.
要是每次因为忘了建目录而导致生产环境挂掉,我都能拿一块钱的话……这还真是个挺常见的低级错误。好在 pathlib 能让你轻松绕过这个坑,不用费太大力气。
Let’s say you want to store sales reports in a reports folder located in your current working directory. You’ll have to create that folder before you can store files in it. You should create it only if it doesn’t exist.
假设您希望将销售报告存储在当前工作目录中的reports文件夹中。您必须先创建该文件夹,然后才能在其中存储文件。只有当它不存在时才应该创建它。
In a nutshell — use mkdir() to create a folder and exists() to check if a folder already exists.
一句话总结:用 mkdir() 创建文件夹,用 exists() 判断文件夹是否已存在。
Here’s the complete code snippet:
下面是完整的代码片段:
Executing the above code will, you’ve guessed it, create a reports folder:
您已经猜到了,执行上述代码将创建一个报表文件夹:
Neat. Let’s see how you can create files in that folder next.
整洁。接下来让我们看看如何在该文件夹中创建文件。
Create files
创建文件
You’d typically save reports through some third-party libraries. Nevertheless, you can also use pathlib to create empty files of any type.
一般保存报告都是靠第三方库,不过用 pathlib 也能直接创建空文件,想建什么类型都行。
Here’s how to create both a CSV and TXT file inside the reports folder:
下面介绍如何在reports文件夹中创建CSV和TXT文件:
The exist_ok=True parameter tells Python to overwrite a file if it already exists.
exist_ok=true参数告诉Python如果某个文件已经存在就覆盖它。
Let’s see if the files were created:
让我们看看是否创建了文件:
Works like a charm.
就像一个符咒。
Check if the path is a folder
检查路径是否为文件夹
If you want to check if a given path is a folder, look no further than the is_dir() function. It returns a boolean.
如果要检查给定路径是否是文件夹,只需查看is_dir()函数即可。它返回一个布尔值。
The following example uses the mentioned function both on a folder and on a file:
以下示例在文件夹和文件上都使用了上述函数:
Here’s what you should see printed:
以下是您应该看到的打印内容:
And that’s all there is to it!
这就是它的全部!
Check if the path is a file
检查路径是否为文件
Similarly to the previous example, you can use the is_file() function to check if a given path results in a file.
与前面的示例类似,您可以使用is_file()函数检查给定路径是否产生文件。
The example below uses it both on a folder and on a file:
下面的示例同时在文件夹和文件上使用它:
As you would imagine, you’ll get the exact opposite results this time:
正如您所想象的那样,这一次您将得到完全相反的结果:
Let’s explore a few more useful functions before calling it a day.
在收工之前,让我们先探索几个更有用的功能。
Get the name of the file
获取文件的名称
You can access the name property if you need to extract a file name from an absolute path.
如果需要从绝对路径中提取文件名,可以访问name属性。
Here’s a simple and not so useful example. It prints the file name of our summer-sales.csv file:
这里有一个简单但不太有用的例子。它打印summer-sales.csv文件的文件名:
Here’s what you should see in the console:
以下是您应该在控制台中看到的内容:
Not much to it.
没什么大不了的。
Get the file extension
获取文件扩展名
Sometimes all you need is a file extension. Maybe you want to process different file types differently, and you don’t care too much about the file name. The suffix property has you covered.
有时你只需要一个文件扩展名。也许你想以不同的方式处理不同的文件类型,而不太关心文件名。后缀属性已经覆盖了您。
Here’s how you can get the file extension from the same summer-sales.csv file:
下面是如何从同一个summer-sales.csv文件获得文件扩展名的:
Here’s what you should see printed out:
下面是应该出现的内容
And finally, let’s cover iteration.
最后,让我们讨论迭代。
Iterate over files in a folder
遍历文件夹中的文件
Let’s say you have a bunch of CSV reports in a single directory, and want to process them one by one. The iterdir() function is all you need.
假设您在一个目录中有一堆CSV报告,并且希望逐个处理它们。您只需要iterdir()函数。
The entire process couldn’t be any simpler:
整个过程再简单不过了:
Here’s what it prints on my machine:
下面是它在我的机器上显示的内容:
And that does it for today. You now have everything needed to never cause a stupid production mistake again.
今天就到此为止了。您现在有了一切所需的东西,再也不会导致愚蠢的生产错误了。
What are your thoughts on pathlib? Is it your favourite file and folder management library, or are you a fan of something else? Let me know in the comment section below.
您对Pathlib有什么想法?它是你最喜欢的文件和文件夹管理库,还是其他东西的粉丝?请在下面的评论区告诉我。
Loved the article? Become a Medium member to continue learning without limits. I’ll receive a portion of your membership fee if you use the following link, with no extra cost to you.
喜欢这篇文章?成为一个不断学习的媒介成员。如果您使用以下链接,我将收到一部分您的会员费,除此之外无额外费用。
Stay connected
保持连接
Follow me on Medium for more stories like this
请访问Medium,了解更多类似的故事
Sign up for my newsletter
订阅我的 newsletter
Connect on LinkedIn
加我 LinkedIn