Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- d@AirBox:/tmp$ cat > random.txt
- These
- Are
- Random
- Words
- That
- Are
- Not
- in
- alphabetical
- order
- d@AirBox:/tmp$ python
- Python 2.7.3 (default, Sep 26 2012, 21:51:14)
- [GCC 4.7.2] on linux2
- Type "help", "copyright", "credits" or "license" for more information.
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readlines()
- ... print f.sorted()
- ...
- Traceback (most recent call last):
- File "<stdin>", line 3, in <module>
- AttributeError: 'list' object has no attribute 'sorted'
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readlines()
- ... print f.sort()
- ...
- None
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readlines().sorted() ]
- ... print f
- ...
- Traceback (most recent call last):
- File "<stdin>", line 2, in <module>
- AttributeError: 'list' object has no attribute 'sorted'
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readlines().sort() ]
- ... print f
- ...
- Traceback (most recent call last):
- File "<stdin>", line 2, in <module>
- TypeError: 'NoneType' object is not iterable
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readline().sort()
- ... print f
- ...
- Traceback (most recent call last):
- File "<stdin>", line 2, in <module>
- AttributeError: 'str' object has no attribute 'sort'
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readlines().sort()
- ... print f
- ...
- None
- >>> with open("/tmp/random.txt","r") as f:
- ... print f.readlines().sort()
- ...
- None
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readlines()
- ... type(f)
- ...
- <type 'list'>
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readlines()
- ... print f
- ...
- ['These\n', 'Are\n', 'Random\n', 'Words\n', 'That\n', 'Are\n', 'Not\n', 'in\n', 'alphabetical\n', 'order\n']
- >>> with open("/tmp/random.txt","r") as f:
- ... f = f.readline()
- ... print f
- ...
- These
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readline() ]
- ... type(f)
- ...
- <type 'list'>
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readline() ]
- ... print f.sorted()
- ...
- Traceback (most recent call last):
- File "<stdin>", line 3, in <module>
- AttributeError: 'list' object has no attribute 'sorted'
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readline() ]
- ... print f.sort()
- ...
- None
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readline() ]
- ... f.sort()
- ... prinf f
- File "<stdin>", line 4
- prinf f
- ^
- SyntaxError: invalid syntax
- >>> f = [ f for f in f.readline() ]
- File "<stdin>", line 1
- f = [ f for f in f.readline() ]
- ^
- IndentationError: unexpected indent
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readline() ]
- ... f.sort()
- ... print f
- ...
- ['\n', 'T', 'e', 'e', 'h', 's']
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f in f.readlines() ]
- ... f.sort()
- ... print f
- ...
- ['Are\n', 'Are\n', 'Not\n', 'Random\n', 'That\n', 'These\n', 'Words\n', 'alphabetical\n', 'in\n', 'order\n']
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f for f.strip("\n") in f.readlines() ]
- ... f.sort
- ... print f
- ...
- File "<stdin>", line 2
- SyntaxError: can't assign to function call
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f.strip"(\n") for f in f.readlines() ]
- File "<stdin>", line 2
- f = [ f.strip"(\n") for f in f.readlines() ]
- ^
- SyntaxError: invalid syntax
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f.strip("\n") for f in f.readlines() ]
- ... f.sort()
- ... print f
- ...
- ['Are', 'Are', 'Not', 'Random', 'That', 'These', 'Words', 'alphabetical', 'in', 'order']
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f.strip("\n") for f in f.readlines() ].sort()
- ... print f
- ...
- None
- >>> with open("/tmp/random.txt","r") as f:
- ... f = [ f.strip("\n") for f in f.readlines().sort() ]
- ... print f
- ...
- Traceback (most recent call last):
- File "<stdin>", line 2, in <module>
- TypeError: 'NoneType' object is not iterable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement