اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی

257 views

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
257
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی

  1. 1. Python for Ethical Hackers Mohammad reza Kamalifard
  2. 2. Python Language Essentials Module 2: System Programming Part 1 : File Handling
  3. 3. File I/O in Python open (file_name,access_mode,buffering) read(byte_count) write(data) close() os.rename() os.remove()
  4. 4. File access modes r Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. w Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing. a Opens a file for appending. 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. Full list : http://www.tutorialspoint.com/python/python_files_io.htm
  5. 5. Creating File >>> file = open('pysec.txt', 'w') >>> file <open file 'pysec.txt', mode 'w' at 0x7f3e7c8dc4b0> >>> for count in range(0,100): ... file.write(str(count) + 'n') ... >>> file.close() >>>
  6. 6. $ cat pysec.txt 0 1 2 3 4 5 6 7 . . . 93 94 95 96 97 98 99
  7. 7. Append to File >>> file = open('pysec.txt', 'a') >>> file <open file 'pysec.txt', mode 'w' at 0x7f3e7c8dc4b0> >>> for count in range(100,200): ... file.write(str(count) + 'n') ... >>> file.close() >>>
  8. 8. $ cat pysec.txt 0 1 2 3 4 5 . . . 191 192 193 194 195 196 197 198 199
  9. 9. read file >>> file = open('pysec.txt','r') >>> for line in file.readlines(): ... print line.strip() ... 0 1 2 3 4 . . 196 197 198 199
  10. 10. rename file and remove file >>> import os >>> os.rename('pysec.txt', 'pysec101.txt') >>> $ $ ls code pysec101.txt pres_file handling.txt os.remove(file_name) >>> import os >>>os.remove('pysec101.txt') $ $ ls code pres_file handling.txt
  11. 11. This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad reza Kamalifard. All rights reserved.

×