Advertisement
MonteVonB

file opening and list sorting scratch.

Jun 8th, 2013
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. d@AirBox:/tmp$ cat > random.txt
  2. These
  3. Are
  4. Random
  5. Words
  6. That
  7. Are
  8. Not
  9. in
  10. alphabetical
  11. order
  12. d@AirBox:/tmp$ python
  13. Python 2.7.3 (default, Sep 26 2012, 21:51:14)
  14. [GCC 4.7.2] on linux2
  15. Type "help", "copyright", "credits" or "license" for more information.
  16. >>> with open("/tmp/random.txt","r") as f:
  17. ...   f = f.readlines()
  18. ...   print f.sorted()
  19. ...
  20. Traceback (most recent call last):
  21.   File "<stdin>", line 3, in <module>
  22. AttributeError: 'list' object has no attribute 'sorted'
  23. >>> with open("/tmp/random.txt","r") as f:
  24. ...   f = f.readlines()
  25. ...   print f.sort()
  26. ...
  27. None
  28. >>> with open("/tmp/random.txt","r") as f:
  29. ...   f = [ f for f in f.readlines().sorted() ]
  30. ...   print f
  31. ...
  32. Traceback (most recent call last):
  33.   File "<stdin>", line 2, in <module>
  34. AttributeError: 'list' object has no attribute 'sorted'
  35. >>> with open("/tmp/random.txt","r") as f:
  36. ...   f = [ f for f in f.readlines().sort() ]
  37. ...   print f
  38. ...
  39. Traceback (most recent call last):
  40.   File "<stdin>", line 2, in <module>
  41. TypeError: 'NoneType' object is not iterable
  42. >>> with open("/tmp/random.txt","r") as f:
  43. ...   f = f.readline().sort()
  44. ...   print f
  45. ...
  46. Traceback (most recent call last):
  47.   File "<stdin>", line 2, in <module>
  48. AttributeError: 'str' object has no attribute 'sort'
  49. >>> with open("/tmp/random.txt","r") as f:
  50. ...   f = f.readlines().sort()
  51. ...   print f
  52. ...
  53. None
  54. >>> with open("/tmp/random.txt","r") as f:
  55. ...    print f.readlines().sort()
  56. ...
  57. None
  58. >>> with open("/tmp/random.txt","r") as f:
  59. ...   f = f.readlines()
  60. ...   type(f)
  61. ...
  62. <type 'list'>
  63. >>> with open("/tmp/random.txt","r") as f:
  64. ...   f = f.readlines()
  65. ...   print f
  66. ...
  67. ['These\n', 'Are\n', 'Random\n', 'Words\n', 'That\n', 'Are\n', 'Not\n', 'in\n', 'alphabetical\n', 'order\n']
  68. >>> with open("/tmp/random.txt","r") as f:
  69. ...   f = f.readline()
  70. ...   print f
  71. ...
  72. These
  73.  
  74. >>> with open("/tmp/random.txt","r") as f:
  75. ...   f = [ f for f in f.readline() ]
  76. ...   type(f)
  77. ...
  78. <type 'list'>
  79. >>> with open("/tmp/random.txt","r") as f:
  80. ...   f = [ f for f in f.readline() ]
  81. ...   print f.sorted()
  82. ...
  83. Traceback (most recent call last):
  84.   File "<stdin>", line 3, in <module>
  85. AttributeError: 'list' object has no attribute 'sorted'
  86. >>> with open("/tmp/random.txt","r") as f:
  87. ...   f = [ f for f in f.readline() ]
  88. ...   print f.sort()
  89. ...
  90. None
  91. >>> with open("/tmp/random.txt","r") as f:
  92. ...   f = [ f for f in f.readline() ]
  93. ...   f.sort()
  94. ...   prinf f
  95.   File "<stdin>", line 4
  96.     prinf f
  97.           ^
  98. SyntaxError: invalid syntax
  99. >>>   f = [ f for f in f.readline() ]
  100.   File "<stdin>", line 1
  101.     f = [ f for f in f.readline() ]
  102.     ^
  103. IndentationError: unexpected indent
  104. >>> with open("/tmp/random.txt","r") as f:
  105. ...   f = [ f for f in f.readline() ]
  106. ...   f.sort()
  107. ...   print f
  108. ...
  109. ['\n', 'T', 'e', 'e', 'h', 's']
  110. >>> with open("/tmp/random.txt","r") as f:
  111. ...   f = [ f for f in f.readlines() ]
  112. ...   f.sort()
  113. ...   print f
  114. ...
  115. ['Are\n', 'Are\n', 'Not\n', 'Random\n', 'That\n', 'These\n', 'Words\n', 'alphabetical\n', 'in\n', 'order\n']
  116. >>> with open("/tmp/random.txt","r") as f:
  117. ...   f = [ f for f.strip("\n") in f.readlines() ]
  118. ...   f.sort
  119. ...   print f
  120. ...
  121.   File "<stdin>", line 2
  122. SyntaxError: can't assign to function call
  123. >>> with open("/tmp/random.txt","r") as f:
  124. ...   f = [ f.strip"(\n") for f in f.readlines() ]
  125.  File "<stdin>", line 2
  126.    f = [ f.strip"(\n") for f in f.readlines() ]
  127.                     ^
  128. SyntaxError: invalid syntax
  129. >>> with open("/tmp/random.txt","r") as f:
  130. ...   f = [ f.strip("\n") for f in f.readlines() ]
  131. ...   f.sort()
  132. ...   print f
  133. ...
  134. ['Are', 'Are', 'Not', 'Random', 'That', 'These', 'Words', 'alphabetical', 'in', 'order']
  135. >>> with open("/tmp/random.txt","r") as f:
  136. ...   f = [ f.strip("\n") for f in f.readlines() ].sort()
  137. ...   print f
  138. ...
  139. None
  140. >>> with open("/tmp/random.txt","r") as f:
  141. ...   f = [ f.strip("\n") for f in f.readlines().sort() ]
  142. ...   print f
  143. ...
  144. Traceback (most recent call last):
  145.  File "<stdin>", line 2, in <module>
  146. TypeError: 'NoneType' object is not iterable
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement