Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

python - 文件对象与路径对象(file object vs path object)

Python 3.4 (and above) has a module called pathlib which is for representing and working with paths.

(Python 3.4(及更高版本)有一个名为pathlib的模块,用于表示和使用路径。)

So, an instance of the pathlib class is an object which represents a path on the system (eg /home/my_file.py).

(因此,pathlib类的实例是一个对象,代表系统上的路径(例如/home/my_file.py)。)

I can use it to read a files contents, write to a file, etc... A file object is a, not surprisingly, an object which represents a file.

(我可以用它来读取文件内容,写入文件等。文件对象是一个代表文件的对象,这并不奇怪。)

I can use it to read a files contents, write to a file, etc...

(我可以用它来读取文件内容,写入文件等...)

So, what is the difference between the two objects?

(那么,这两个对象有什么区别?)

Why choose one over the other?

(为什么要选择一个?)

Obviously, a file object isn't used to make copies or check the current working directory so there are reasons to choose pathlib when we need cerain behaviours.

(显然,文件对象不用于复制或检查当前工作目录,因此有必要在我们需要某些习惯时选择pathlib。)

However, it seems pathlib does everything I might want a file object to do so why bother with file objects?

(但是,似乎pathlib会执行我可能希望文件对象执行的所有操作,为什么还要麻烦文件对象?)

  ask by Aaron Zolotor translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...