मैं चाहता हूँ:कैसे छंटनी (पढ़ना-लिखना) खोलना या छंटनी के साथ फाइल बनाना है?
- पढ़ने-लिखने की विधा में एक फ़ाइल खोलने यदि वह मौजूद है;
- यदि यह अस्तित्व में नहीं है तो इसे बनाएं;
- इसे कभी भी कहीं भी छीनने में सक्षम हो।
संपादित: truncate मैं एक स्थिति तक लिख सकते हैं और फ़ाइल के शेष भाग को निरस्त मतलब के साथ, अगर वर्तमान
सभी इस atomically (एक open()
कॉल या एक ही open()
कॉल अनुकरण के साथ)
कोई भी खुला साधन लागू करने के लिए लगता है:
- r: स्पष्ट रूप से काम नहीं करता है;
- आर +: विफल रहता है अगर फ़ाइल मौजूद नहीं है;
- डब्ल्यू: अगर यह मौजूद है तो फाइल को फिर से बनाएं;
- डब्ल्यू +: अगर यह मौजूद है तो फाइल को फिर से बनाएं;
- ए: पढ़ नहीं सकता;
- ए +: छोटा नहीं कर सकता।
कुछ संयोजन मैंने कोशिश की (आरडब्ल्यू, आरडब्ल्यू +, आर + डब्ल्यू, इत्यादि) या तो काम नहीं कर रहे हैं। क्या यह संभव है?
कुछ doc रूबी से (भी अजगर पर लागू होता है):
r
Read-only mode. The file pointer is placed at the beginning of the file.
This is the default mode.
r+
Read-write mode. The file pointer will be at the beginning of the file.
w
Write-only mode. Overwrites the file if the file exists. If the file
does not exist, creates a new file for writing.
w+
Read-write mode. Overwrites the existing file if the file exists. If the
file does not exist, creates a new file for reading and writing.
a
Write-only mode. The file pointer is at the end of the file if the file
exists. That is, the file is in the append mode. If the file does not exist,
it creates a new file for writing.
a+
Read and write mode. The file pointer is at the end of the file if the file
exists. The file opens in the append mode. If the file does not exist, it
creates a new file for reading and writing.
तो तुम अनिवार्य रूप से एक अधिलेखित करना चाहते हैं फ़ाइल करें और सुनिश्चित करें कि पिछली सामग्री उस बिंदु से नीचे नहीं रहती है जिसे आपने लिखना बंद कर दिया है? –
हां। 'file.truncate() 'इस उद्देश्य के लिए काम करता है और यह तब काम करता है जब फ़ाइल" आर + "," डब्ल्यू "," डब्ल्यू + "होती है। लेकिन उनमें से सभी को ऊपर सूचीबद्ध दोष हैं। – ceztko