What are the commonly used functions in Python?
shutil 是 Python 中的高级文件操作模块,与os模块形成互补的关系,os主要提供了文件或文件夹的新建、删除、查看等方法,还提供了对文件以及目录的路径操作。shutil模块提供了移动、复制、 压缩、解压等操作,恰好与os互补,共同一起使用,基本能完成所有文件的操作。是一个非常重要的模块。
#加载包 import shutil #查看包中的所有方法 print(dir(shutil)) [ 'chown', 'collections', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copymode', 'copystat', 'copytree', 'disk_usage', 'errno', 'fnmatch', 'get_archive_formats', 'get_terminal_size', 'get_unpack_formats', 'getgrnam', 'getpwnam', 'ignore_patterns', 'make_archive', 'move', 'nt', 'os', 'register_archive_format', 'register_unpack_format', 'rmtree', 'stat', 'sys', 'unpack_archive', 'unregister_archive_format', 'unregister_unpack_format', 'which']
01、copy()
描述:复制文件
语法:shutil.copy(fsrc,path),返回值:返回复制之后的路径
fsrc:源文件
path:目标地址
shutil.copy('test.csv','C:/Users/zhengxiang.wzx/Desktop/') 'C:/Users/zhengxiang.wzx/Desktop/test.csv'
02、copy2()
描述:复制文件和状态信息
语法:shutil.copy(fsrc,path),返回值:返回复制之后的路径
fsrc:源文件
path:目标地址
shutil.copy2('test.csv','C:/Users/zhengxiang.wzx/Desktop/') 'C:/Users/zhengxiang.wzx/Desktop/test.csv'
03、copyfileobj()
描述:将一个文件的内容拷贝到另一个文件中,如果目标文件本身就有内容,来源文件的内容会把目标文件的内容覆盖掉。如果文件不存在它会自动创建一个。
语法:shutil.copyfileobj(fsrc, fdst[, length=16*1024])
fsrc:源文件
fdst:复制至fdst文件
length:缓冲区大小,即fsrc每次读取的长度
import shutil f1 = open('file.txt','r') f2 = open('file_copy.txt','w+') shutil.copyfileobj(f1,f2,length=16*1024)
04、copyfile()
描述:将一个文件的内容拷贝到另一个文件中,目标文件无需存在
语法:shutil.copyfile(src, dst,follow_symlinks)
src:源文件路径
dst:复制至dst文件,若dst文件不存在,将会生成一个dst文件;若存在将会被覆盖
follow_symlinks:设置为True时,若src为软连接,则当成文件复制;如果设置为False,复制软连接。默认为True。
import shutil f1 = open('file.txt','r') f2 = open('file_copy.txt','w+') shutil.copyfileobj(f1,f2,length=16*1024)
05、copytree()
描述:复制整个目录文件,不需要的文件类型可以不复制
语法:shutil.copytree(oripath, despath, ignore= shutil.ignore_patterns(".xls", ".doc"))
参数:
oripath : “来源路径”
despath : “目标路径”
ignore : shutil.ignore_patterns() 是对内容进行忽略筛选,将对应的内容进行忽略。
import shutil,os path2 = os.path.join(os.getcwd(),"kaggle") path2 'C:\\Users\\wuzhengxiang\\Desktop\\Python知识点总结\\kaggle' #bbb与ccc文件夹都可以不存在,会自动创建 path3 = os.path.join(os.getcwd(),"bbb","ccc") path3 'C:\\Users\\wuzhengxiang\\Desktop\\Python知识点总结\\bbb\\ccc' # 将"abc.txt","bcd.txt"忽略,不复制 shutil.copytree(path2,path3,ignore=shutil.ignore_patterns("abc.txt","bcd.txt"))
06、copymode()
描述:拷贝权限,前提是目标文件存在,不然会报错。将src文件权限复制至dst文件。文件内容,所有者和组不受影响
语法:shutil.copymode(src,dst)
src:源文件路径
dst:将权限复制至dst文件,dst路径必须是真实的路径,并且文件必须存在,否则将会报文件找不到错误
follow_symlinks:设置为False时,src, dst皆为软连接,可以复制软连接权限,如果设置为True,则当成普通文件复制权限。默认为True。Python3新增参数
shutil.copymode("file_0.csv","file_1.csv")
07、move()
描述:移动文件或文件夹
语法:shutil.move(src, dst)
os.chdir('C:/Users/wuzhengxiang/Desktop/Python知识点总结') os.getcwd() shutil.move('file_1.csv', 'C:/Users/wuzhengxiang/Desktop/股票数据分析') 'C:/Users/wuzhengxiang/Desktop/股票数据分析\\file_1.csv'
08、disk_usage()
描述:查看磁盘使用信息,计算磁盘总存储,已用存储,剩余存储信息。
语法:shutil.disk_usage(‘盘符’)
返回值:元组
shutil.disk_usage('D:') usage(total=151199412224, used=41293144064, free=109906268160) total,总存储:151199412224/1024/1024/1024=140GB used,已使用:41293144064/1024/1024/1024=38GB free,剩余容量:109906268160/1024/1024/1024=102GB
09、 make_archive()
描述:压缩打包
语法:make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None)
压缩打包
base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径
format: 压缩或者打包格式 “zip”, “tar”, "bztar"or “gztar”
root_dir : 将哪个目录或者文件打包(也就是源文件)
#把当前目录下的file_1.csv打包压缩 shutil.make_archive('file_1.csv','gztar',root_dir='C:/Users/wuzhengxiang/Desktop/股票数据分析') 'C:\\Users\\wuzhengxiang\\Desktop\\股票数据分析\\file_1.csv.tar.gz'
09、 get_archive_formats()
描述: 获取支持的压缩文件格式。目前支持的有:tar、zip、gztar、bztar。在Python3还多支持一种格式xztar
在学习Python的过程中,往往因为没有资料或者没人指导从而导致自己不想学下去了,因此我特意准备了个群 827513319 ,群里有大量的PDF书籍、教程都给大家免费使用!不管是学习到哪个阶段的小伙伴都可以获取到自己相对应的资料!
语法:unpack_archive(filename, extract_dir=None, format=None)
filename:文件路径
extract_dir:解压至的文件夹路径。文件夹可以不存在,会自动生成
format:解压格式,默认为None,会根据扩展名自动选择解压格式
import shutil,os zip_path = os.path.join(os.getcwd(),"file_1.csv.tar") extract_dir = os.path.join(os.getcwd(),"aaa") shutil.unpack_archive(zip_path, extract_dir)
10、rmtree()
描述:递归的去删除文件
语法:shutil.rmtree(path[, ignore_errors[, onerror]])
#删除文件夹shutil.rmtree('C:/Users/wuzhengxiang/Desktop/Python知识点总结/test2')
The above is the detailed content of What are the commonly used functions in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PS "Loading" problems are caused by resource access or processing problems: hard disk reading speed is slow or bad: Use CrystalDiskInfo to check the hard disk health and replace the problematic hard disk. Insufficient memory: Upgrade memory to meet PS's needs for high-resolution images and complex layer processing. Graphics card drivers are outdated or corrupted: Update the drivers to optimize communication between the PS and the graphics card. File paths are too long or file names have special characters: use short paths and avoid special characters. PS's own problem: Reinstall or repair the PS installer.

A PS stuck on "Loading" when booting can be caused by various reasons: Disable corrupt or conflicting plugins. Delete or rename a corrupted configuration file. Close unnecessary programs or upgrade memory to avoid insufficient memory. Upgrade to a solid-state drive to speed up hard drive reading. Reinstalling PS to repair corrupt system files or installation package issues. View error information during the startup process of error log analysis.

"Loading" stuttering occurs when opening a file on PS. The reasons may include: too large or corrupted file, insufficient memory, slow hard disk speed, graphics card driver problems, PS version or plug-in conflicts. The solutions are: check file size and integrity, increase memory, upgrade hard disk, update graphics card driver, uninstall or disable suspicious plug-ins, and reinstall PS. This problem can be effectively solved by gradually checking and making good use of PS performance settings and developing good file management habits.

The article introduces the operation of MySQL database. First, you need to install a MySQL client, such as MySQLWorkbench or command line client. 1. Use the mysql-uroot-p command to connect to the server and log in with the root account password; 2. Use CREATEDATABASE to create a database, and USE select a database; 3. Use CREATETABLE to create a table, define fields and data types; 4. Use INSERTINTO to insert data, query data, update data by UPDATE, and delete data by DELETE. Only by mastering these steps, learning to deal with common problems and optimizing database performance can you use MySQL efficiently.

The key to feather control is to understand its gradual nature. PS itself does not provide the option to directly control the gradient curve, but you can flexibly adjust the radius and gradient softness by multiple feathering, matching masks, and fine selections to achieve a natural transition effect.

MySQL performance optimization needs to start from three aspects: installation configuration, indexing and query optimization, monitoring and tuning. 1. After installation, you need to adjust the my.cnf file according to the server configuration, such as the innodb_buffer_pool_size parameter, and close query_cache_size; 2. Create a suitable index to avoid excessive indexes, and optimize query statements, such as using the EXPLAIN command to analyze the execution plan; 3. Use MySQL's own monitoring tool (SHOWPROCESSLIST, SHOWSTATUS) to monitor the database health, and regularly back up and organize the database. Only by continuously optimizing these steps can the performance of MySQL database be improved.

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

The loading interface of PS card may be caused by the software itself (file corruption or plug-in conflict), system environment (due driver or system files corruption), or hardware (hard disk corruption or memory stick failure). First check whether the computer resources are sufficient, close the background program and release memory and CPU resources. Fix PS installation or check for compatibility issues for plug-ins. Update or fallback to the PS version. Check the graphics card driver and update it, and run the system file check. If you troubleshoot the above problems, you can try hard disk detection and memory testing.
