Python action file

Source: Internet
Author: User

Read/write

You can use modes such as W,A,R,WB,AB,RB

With open (' xxx.xx ', ' WB ') as F:f.write (' xxxxxxxxx ')

Copy

If destination is a folder, the source file is copied to destination and the original file name is maintained

If destination is the end of the filename, it will be the new name of the copied file

Shutil.copy (source, destination)

Move

If destination is a folder, the source file is moved to destination and the original file name is maintained

If the source file already exists in the destination folder, the file with the same name in the destination folder will be overwritten

If there is no destination folder, the source will be renamed to destination

Shutil.move (source, destination)

Delete

Os.unlink (file) Delete file files (individual files)

Os.rmdir (path) deletes the path folder, which must be empty, without any files and subfolders (single empty folder)

Shutil.rmtree (path) deletes the path folder, all files and subfolders it contains are deleted

Send2trash.send2trash (file) sends folders and files to the computer's trash or Recycle Bin, rather than permanently deleting them

The Os.walk () function is passed in a string value, which is the path to a folder. You can have a for

Os.walk () traverses the directory tree, returning 3 values:

1. The name of the folder that is currently traversed;

2. A list of subfolders in the current folder;

3. The list of files in the current folder.

Compression

# New compressed file is opened in write mode, and write mode erases all the original contents in the zip file

# The second argument passed in ' a ' is opened in Add mode, the file is added to the original ZIP file,

>>> newzip = ZipFile. ZipFile (' New.zip ', ' W ')

The # Write () method passes the first parameter to the file, compresses the file, adds it to the zip file, and the second parameter is "compression type", which tells the computer what algorithm to use to compress the file

>>> newzip.write (' Spam.txt ', compress_type=zipfile. zip_deflated)

>>> Newzip.close ()

Open compressed file

>>> examplezip = ZipFile. ZipFile (' Example.zip ')

# View a list of all files and folders in a compressed file

>>> examplezip.namelist ()

[' Spam.txt ', ' cats/', ' cats/catnames.txt ', ' cats/zophie.jpg ']

# returns the Zipinfo object for the specified file, Zipinfo object has its own properties

>>> spaminfo = examplezip.getinfo (' spam.txt ')

# Original File size

>>> spaminfo.file_size

13908

# File size after compression

>>> spaminfo.compress_size

3828

>>> ' compressed file is%sx smaller! '% (round (spaminfo.file_size/spaminfo.compress_size, 2))

' Compressed file is 3.63x smaller! '

>>> Examplezip.close ()

Extract

# Extractall () unzip all files and folders from the zip file and put them in the current working directory

>>> examplezip = ZipFile. ZipFile (' Example.zip ')

>>> Examplezip.extractall ()

# Pass the folder name to Extractall () and the file will be extracted to the specified directory

>>> examplezip.extractall (' c:\\ delicious ')

>>> Examplezip.close ()

# extract () extract a single file from the zip file, the second parameter is extracted to the specified directory, if the specified folder does not exist, it is automatically created

>>> examplezip.extract (' Spam.txt ')

' C:\\spam.txt '

>>> examplezip.extract (' spam.txt ', ' c:\\some\\new\\folders ')

' C:\\some\\new\\folders\\spam.txt '

>>> Examplezip.close ()


Python action file

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.