简体   繁体   English

打开和关闭文件python的目的

[英]Purpose of opening and closing files python

What is the logic behind python requiring that a file be opened for write or append before being written to? python背后的逻辑是什么要求在写入之前打开文件进行写入或追加? Is there an advantage of doing this compared with something like bash which can just directly write to the file with something like: print 'Hello World' >> output.txt ? 与bash这样的东西相比,有什么优势可以直接写入文件,例如: print 'Hello World' >> output.txt

Bash command is actually a small script or program, it's supposed to provide end to end operations. Bash命令实际上是一个小脚本或程序,它应该提供端到端操作。

For programming, you'll use more basic unit operations (open, close, read, write, rewind, seek) than a set of operations (copy, delete). 对于编程,您将使用比一组操作(复制,删除)更多的基本单元操作(打开,关闭,读取,写入,回放,搜索)。 It's true both for Python, C , etc. 对于Python, C等都是如此。

If you go deeper into operating system coding, you will probably need operations to manipulate file representation details such as file handler and storage system. 如果深入了解操作系统编码,您可能需要操作来操作文件表示细节,例如文件处理程序和存储系统。

It's different level of abstraction for handle problems of different granularity. 对于不同粒度的句柄问题,它是不同的抽象级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM