Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize File clipping and Compression by using shutil Module in Python

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the "Python how to use the shutil module to achieve file cutting and compression" related knowledge, in the actual case operation process, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Using shutil to realize file clipping (moving, renaming)

Import packages and modules

From shutil import move

How to use it:

Move (source address, destination address)

In conjunction with our test script scenario in the previous section, move abcd.txt to the test01 directory, and then rename abcd_copy.txt to efg_copy.txt in the current directory.

The code example is as follows:

# coding:utf-8import osfrom shutil import movepath = os.path.join (os.getcwd (), 'abcd.txt') # use the getcwd () function of the os module to obtain the absolute path target = os.path.join (os.getcwd (),' test01') move (path, target) # move the "abcd.txt" file to the "test01" directory move ('abcd_copy.txt') 'efg_copy.txt') # rename "abcd_copy.txt" to "efg_copy.txt"

The running results are as follows:

Deletion of files

Here we will use the remove () function of the os package to delete files. There is no separate file deletion function in the shutil package, because the deletion of the os package is detailed enough.

Next, let's consolidate the remove () function of the os package and use the remove () function to delete the efg.txt file with our renamed name above:

# coding:utf-8import osfrom shutil import copy, copyfile, movepath = os.path.join (os.getcwd (), 'abcd.txt') # use the getcwd () function of the os module to obtain the absolute path target = os.path.join (os.getcwd (),' test01') # copyfile (path, target) # move (path, target) # move the "abcd.txt" file to the "test01" directory # move ('abcd_copy.txt') 'efg_copy.txt') # rename "abcd_copy.txt" to "efg_copy.txt" os.remove (' efg_copy.txt')

The running results are as follows:

Using shutil to realize File Compression

Let's first look at the compressed function:

Import packages and modules

From shutil import make_archive

Usage

Make_archive (the name of the compressed file, the suffix of the compressed file, the file or directory that you want to be compressed)

Return value

Generated compressed packet address

Import osfrom shutil import copy, copyfile, move, make_archivepath = os.path.join (os.getcwd (), 'abcd.txt') # use the getcwd () function of the os module to obtain the absolute path target = os.path.join (os.getcwd (),' test01') make_archive ('test01',' zip', os.path.join (os.getcwd (), 'test01'))

The running results are as follows:

Using shutil to decompress files

Let's look at the unzipped function again:

Import packages and modules

From shutil import unpack_archive

Usage

Unpack_archive (the file to be extracted, the unzipped path)

Return value

Generated compressed packet address

The code example is as follows:

# coding:utf-8import osfrom shutil import copy, copyfile, move, make_archive, unpack_archive# path = os.path.join (os.getcwd (), 'abcd.txt') # use the getcwd () function of the os module to obtain the absolute path # target = os.path.join (os.getcwd (),' test01') # make_archive ('test01',' zip', os.path.join (os.getcwd ()) 'test01')) target = os.path.join (os.getcwd (),' test02') # in order to distinguish the extracted directory name from the previous test01 Test02 is used here as the extracted directory unpack_archive ('test01.zip', target)

The running results are as follows:

"Python how to use the shutil module to achieve file clipping and compression" content is introduced here, thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report