Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open files in "rock&roll" mode

I'm wondering what is going on with the file open() mode validation (Python2.7):

>>> with open('input.txt', 'illegal') as f:
...     for line in f:
...         print line
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: mode string must begin with one of 'r', 'w', 'a' or 'U', not 'illegal'

>>> with open('input.txt', 'rock&roll') as f:
...     for line in f:
...         print line
... 
1

2

3

So, I cannot open the file in illegal mode, but I can open it in rock&roll mode. What mode is actually used for opening the file in this case?

Note that on python3 I cannot use both illegal and rock&roll:

>>> with open('input.txt', 'rock&roll') as f:
...     for line in f:
...         print(line)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid mode: 'rock&roll'
>>> with open('input.txt', 'illegal') as f:
...     for line in f:
...         print(line)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid mode: 'illegal'

And, this is confusing, why the behavior is different for python3.x?

like image 837
alecxe Avatar asked Apr 14 '14 03:04

alecxe


People also ask

How do I open a rock file?

You need a suitable software like Rockbox from The Rockbox Crew to open a ROCK file. Without proper software you will receive a Windows message "How do you want to open this file?" or "Windows cannot open this file" or a similar Mac/iPhone/Android alert.

What is a. rocks file?

Plug-in file used by Rockbox, an open source firmware for portable music players; stores a game or another small application that can run on Rockbox; enables Rockbox "apps" to be used on iPods, SanDisk MP3 players, and other portable music players.


1 Answers

The Python 2.x open function essentially delegates its work to the C library fopen function. On my system, the documentation for fopen contains:

The argument mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):

Your ock&roll is considered "additional characters".

In Python 3, the allowed open modes are more restricted (essentially, only valid strings are permitted).

like image 52
Greg Hewgill Avatar answered Oct 06 '22 09:10

Greg Hewgill