Python Base Four, pythonbasefour

Source: Internet
Author: User

Python Base Four, pythonbasefour

35. In python, file operation syntax is similar to c.

Open (file, 'R ',......) // The first parameters is necessary, other is optional, the second parameters is 'R' by default

If you want to open a file, you can use:

F = open ('// Users // wyg_mac // Desktop // Account.txt') // if you don't know the path, you can drag the file to console

If you want to close file, you can use:

F. close ()

Next now that we know how to open file and close file, but how to read file content.

You can use

F. read (size =-1) // read size buffer, if size not define or minus, read next buffer, and return as string

F. readline () // open as write mode, if file exist, append at end

If you want to write content to file, how to achieve it.

F. wirte (str) // put str write file

F. wirtelines (seq )//

F. tell () // return current position

F. seek (offset, from) // from (if is 0, start position, 1 current position 2 end position) offset

Such as if I want to create a file demo.txt on desktop and write content whth 'write code change world'

F = open (R'/users/wyg_mac/desktop/demo.txt ', 'w ')

F. wirte ('write code change World ')

F. close ()

There are something we need to notice:

If a file not exist, as follow, it will have error:

F = open (R'/users/wyg_mac/desktop/a.txt ')-> error

If file not exist, you use follow it will be right:

F = open (R'/users/wyg_mac/desktop/a.txt ')-> right

 

// Seventh day to study python

36. In python, OS module is base and important.

Import OS

OS. getcwd () // get current file path

-> '/Users/wyg_mac/documents'

OS. chdir ('/users/wyg_mac/desktop') // change file path

OS. getcwd ()

-> '/Users/wyg_mac/desktop

OS. listdir ('/users/wyg_mac/desktop') // list file dir

-> Choose 'demo.txt ','. DS_Store ',]

OS. mkdir ('/users/wyg_mac/desktop/')

-> Create a file named

OS. mkdir ('/users/wyg_mac/desktop/a/B ')

-> If file a exist, create file named B in file a, if a not exist, will have an error

OS. makedirs ('/users/wyg_mac/desktop/m/n/o ')

-> Create file m on desktop, m include n, n include o

If file m/n/o, o include ttt.txt

OS. remove ('/users/wyg_mac/desktop/m/o/ttt.txt ')

-> Remove ttt.txt according to path

OS. rmdir ('/users/wyg_mac/desktop/m/n/o ')

-> Remove o file, if o contain content, error, if not, remove it.

OS. removedirs ('path ')

->

OS. rename ('/users/wyg_mac/desktop/A','/users/wyg_mac/desktop/aaa ')

-> Change file name

OS. system (command)

-> Command is shell command, such as OS. system ('cmd ')

OS. curdir // current

->.

OS. pardir //

-> ..

Such:

OS. listdir (OS. curdir '= OS. listdir ('.')

OS. sep // current OS path sep

->/

OS. name // current OS name

-> 'Posix'

 

37. OS. path

OS. path. bastname ('/users/wyg_mac ')

-> Wyg_name

OS. path. dirname ('/users/wyg_mac/desktop ')

-> Users/wyg_mac

OS. path. join ('/users', 'wyg _ mac', 'desktop ')

-> '/Users/wyg_mac/desktop'

OS. path. split ('/users/wyg_mac/desktop/m/N ')

-> ('/Users/wyg_mac/desktop/m', 'n ')

OS. path. splitext ('/users/wyg_mac/desktop/m ')

-> ('/Users/wyg_mac/desktop/m ','')

OS. path. splitext ('/users/wyg_mac/desktop/m.txt ')

-> ('/Uses/wyg_mac/desktop/m', '.txt ')

OS. path. getsize ('/users/wyg_mac/desktop/m.txt ')

-> 35

OS. path. getctime ()

OS. path. getmtime ()

OS. path. getatime ()

OS. path. mtime ('/users/wyg_mac/desktop/m ')

-> 1470748934.0

If you don't understand it, you can use:

Import time

Time. localtime (OS. path. getmtime ('/users/wyg_mac/desktop/m '))

-> Time. struct_time (tm_year = 2016, tm_mon = 8, tm_mday = 9, tm_hour = 21, tm_min = 22, tm_sec = 14, tm_wday = 1, tm_yday = 222, tm_isdst = 0

OS. path. exists ('/users/wyg_mac/desktop/m ')

-> True

OS. path. isabs () // if abs path

OS. path. isfile ()

OS. path. isdir ()

OS. path. ismount ()

OS. path. islink ()

OS. path. samefile (,)

 

38. pickle module, you can store data to file and read it

Import pickle

My_list = [123, 3.14, 'roy '[1, 3]

Pickle_file = open('my_list.txt ', 'wb ')

Pickle. dump (my_list, pickle_file)

Pickle_file.close ()

 

Pickle_file = open('my_list.txt ', 'rb ')

My_list2 = pickle. load (pickle_file)

Print (my_list2)

-> [123, 3.14, 'roy ', [1, 3]

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.