Python Learning Notes (children under five) deep, shallow copies, files and folders

Source: Internet
Author: User
Tags shallow copy

Python Learning Notes (children under five)


deep copy-shallow copy

a shallow copy is a copy of the reference (copying only the parent object)
a deep copy is a resource copy of an object.

normal replication simply adds a "label" to the same address space, and everything is the same. Change or add, the copied variables will change
.
Examples:
>>> a=[1,2,3]
>>> B=a
>>> A
[1, 2, 3]
>>> b
[1, 2, 3]
>>> a[1]=0
>>> A
[1, 0, 3]
>>> b
[1, 0, 3]
>>> A.append (4)
>>> A
[1, 0, 3, 4]
>>> b
[1, 0, 3, 4]

Shallow Copy

To add Copy module import copy

A shallow copy of the data will not be copied in a separate copy, just a copy of the reference, each element of the ID is the same

Examples:

>>> Import Copy

>>> a=[1,2,[' A ', ' B ', ' C ']
>>> B=copy.copy (a)
>>> b
[1, 2, [' A ', ' B ', ' C ']

>>> ID (a)
48145736L
>>> ID (b)
48123592L
>>> ID (a[0])
30730152L
>>> ID (b[0])
30730152L
>>> a.append (0)
>>> A
[1, 2, [' A ', ' B ', ' C '], 0]
>>> b
[1, 2, [' A ', ' B ', ' C ']
>>> a[2].append (' d ')
>>> A
[1, 2, [' A ', ' B ', ' C ', ' d '], 0]
>>> b
[1, 2, [' A ', ' B ', ' C ', ' d ']]


Deep Copy
simply changing the copy.copy () to a copy.deepcopy () deep copy will put all the data in a single copy

read and write files

can use the Open function. or file Class
Open (name[, mode[, buffering]) file (name[, mode[, buffering])
fo = Open ("/code/1.txt") #使用open函数print Fo.read () Fo.close () f1 = File ("/code/1.txt") #使用file类print F1.read () f1.close () 

mode
R   &NB SP; Read r+   read-write only. Assumption already exists. Writes W   writes from the file pointer position, assuming that it already exists, deleting the original file, and then writing w+  read and write again, assuming that it already exists, delete the original file first. Write a     write again. Append new content to the end of file A +   read and write, append new content at the end of the file B     open binary. Ability to use U   in combination with R, W, a, + to support all line break symbols. "\ r" "\ n"   "\ r \ n"
Sample: Write
#/usr/bin//usr/bin/python2.7#-*-Coding:utf-8-*-fnew = open ('/code/new.txt ', ' W ') fnew.write (' Hello python!! ')  #这时2还没有真正的写到文件里去, just in the buffer. Close or flush before writing to the file Fnew.close ()

File Object Methods

string = Fileobject.read ([size]) reads, starting from the file pointer to read, which allows a size to specify how much string = Fileobject.readline ([size]) reads a row at a time. And the file pointer to the next line list = Fileoject.readlines ([size]) returns a list of characters that hold no Line Fileobject.next () returns the current row, and the file file pointer to the next line is different from ReadLine. ReadLine returns an empty string after all the rows have been read, and next warns
Fileobject.write (String) write
Fileobject.writelines (list) multiple-line writes. Higher efficiency than write, faster

Fileobject.seek (offset, option)
option = 0 Move the file pointer to the file header + offset
option = 1 Move the file pointer to the current position + offset
option = 2 Move the file pointer to the end-at offset
Fileobject.flush () Submit the update. Write the buffer to the file.
Sample: Find
Import REFP = File (' 1.txt ', ' r ') Count = 0for s in Fp.readlines ():      li = Re.findall (' hello ', s)      if Len (li) >0:            Count = Count + len (LI) print "Search" +str (count) + "Hello" fp.close ()

Example: Replace
#-*-coding:utf-8-*-import refp = file (' 1.txt ', ' r+ ') S=fp.read () Fp.seek (0,0) fp.truncate ()    #文件清空fp. Write ( S.replace (' Hello ', ' Hi ')) fp.close ()

OS module

Introduction of OS module import OS
folder Operations through the OS module to create folders, changes, traversal and other create folders (cannot create multi-level folders) Os.mkdir (path [, mode=0777]) Create a folder (can create multi-level folders) Os.makedirs (path [, mode=0777]) Delete folder (cannot delete multilevel folder) Os.rmdir (path) Delete folder (can delete multilevel folder) Os.removedirs (path)
List Folder Os.listdir (path) Get current folder OS.GETCWD () Toggle folder Os.chdir (path)


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Python Learning Notes (children under five) deep, shallow copies, files and folders

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.