Asked  7 Months ago    Answers:  5   Viewed   46 times

From the doc,

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text files; on systems that don’t have this distinction, adding the 'b' has no effect.

and here

w+ : Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

But, how to read a file open with w+?



Let's say you're opening the file with a with statement like you should be. Then you'd do something like this to read from your file:

with open('somefile.txt', 'w+') as f:
    # Note that f has now been truncated to 0 bytes, so you'll only
    # be able to read data that you write after this point
    f.write('somedatan')  # Important: return to the top of the file before reading, otherwise you'll just read an empty string
    data = # Returns 'somedatan'

Note the -- if you forget this, the call will try to read from the end of the file, and will return an empty string.

Tuesday, June 1, 2021
answered 7 Months ago

Your newlines are coming from the print function


import sys

sys.stdout.write ('some stuff')

and your line breaks will go away

Wednesday, August 11, 2021
answered 4 Months ago

Django's developers have a slightly non-traditional view on the MVC paradigm. They actually address this question in their FAQs, which you can read here. In their own words:

In our interpretation of MVC, the “view” describes the data that gets presented to the user. It’s not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. It’s a subtle distinction.

So, in our case, a “view” is the Python callback function for a particular URL, because that callback function describes which data is presented.

Furthermore, it’s sensible to separate content from presentation – which is where templates come in. In Django, a “view” describes which data is presented, but a view normally delegates to a template, which describes how the data is presented.

Where does the “controller” fit in, then? In Django’s case, it’s probably the framework itself: the machinery that sends a request to the appropriate view, according to the Django URL configuration.

Wednesday, August 11, 2021
Matthew J Morrison
answered 4 Months ago

the files are names of file objects in root directory.

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

try this

for root, dirs, files in os.walk("/users/home10/tshrestha/brb-view/logs/vdm-sdct-agent/pcoip-logs"):
    lineContainsServerFile = re.compile('.*server.*')
    for filename in files:
            if lineContainsServerFile.match(filename):
                    filename = os.path.join(root, filename)
                    with open(filename,'rb') as files:
                            print 'filename:', filename
Sunday, August 29, 2021
answered 4 Months ago

As an addition to @Jarret Hardie's answer here's how Python check file mode in the function fileio_init():

s = mode;
while (*s) {
    switch (*s++) {
    case 'r':
        if (rwa) {
                    "Must have exactly one of read/write/append mode");
            goto error;
        rwa = 1;
        self->readable = 1;
    case 'w':
        if (rwa)
            goto bad_mode;
        rwa = 1;
        self->writable = 1;
        flags |= O_CREAT | O_TRUNC;
    case 'a':
        if (rwa)
            goto bad_mode;
        rwa = 1;
        self->writable = 1;
        flags |= O_CREAT;
        append = 1;
    case 'b':
    case '+':
        if (plus)
            goto bad_mode;
        self->readable = self->writable = 1;
        plus = 1;
                 "invalid mode: %.200s", mode);
        goto error;

if (!rwa)
    goto bad_mode;

That is: only "rwab+" characters are allowed; there must be exactly one of "rwa", at most one '+' and 'b' is a noop.

Friday, September 10, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :