current position:Home>Python notes - Day11 - package and file operations
Python notes - Day11 - package and file operations
2022-05-15 06:03:29【Wandering mage 12】
Preface
python Grammar learning , Leave it to those who need it , Understand everything !!
# coding=utf8
# @time:2022/4/20 15:04
# Author Haoyu
# One 、 The operation of the package
# 1. What is a bag
# The package is equivalent to a folder ( The module is equivalent to a file ), Packages are used to manage modules ;
'''''''''
Bag is python It is specially used to manage py File folder , There is a special file in this folder ”__init__.py
( The normal folder in the project is generally used to manage the non code files needed in the project , Such as : Audio 、 Video etc. )
'''''''''
# 2. How to use the contents of the package - Import
'''''''''
1)import package - After importing, you can use ‘ Package name .’ Use the... In this package .py All global variables defined in the file
2)import Package name . Module name - After importing, you can use ‘ Package name . Module name .’ To use all global variables in the specified module
3)import Modules in the package as New name - Directly rename the modules in the imported package
4)from Package name import Module name - Import the specified module in the specified package , After importing, you can use ‘ Module name .’ To use all the global variables in the module
5)from Package name . Module name import Variable 1, Variable 2,.... - Import the specified variables in the specified module , Variables are used directly when they are used
'''''''''
# 1) Direct import package
# import filemannger
# print(filemannger.x)
# 2) Directly import the modules in the package
# import filemannger.source
# filemannger.source.read_file() # Reading documents
# 3) Directly rename the modules in the imported package
# Be careful : If you call every time filemannger.source It will be very trouble , Here can be combined with renaming , If it is renamed as f1, After Mina can directly use f1
# import filemannger.source as f1
# f1.read_file() # Reading documents
# 4) Import modules directly through packages
# from filemannger import source
# source.read_file() # Reading documents
# source.read_file() # Reading documents
# 5) Directly import the variables in the module in the package
# from filemannger.source import x
# print(x) # 100
# 3.__init__ file
# 1) When importing a package, specify the default imported module or variables in the module
# from filemannger.source import x
#
# 2) Define some general functions
# def common():
# print(' The generic function ')
# Two 、 File operations
# File operations -> Operation file content
# 1. Data persistence
# The data used and generated in the program are saved in the running memory by default , When the program ends, the data saved in the running memory will be automatically destroyed .
# If you want the data not to be destroyed after the program ends , Store data on the hard disk .
# Save the data to the hard disk , Data persistence .( Be careful : Data cannot be put directly into the hard disk , Must be saved by file )
# Common file types when programming :txt、json、plist、 database ( The most commonly used )
# 2. File operations
'''''''''
1) Basic steps : Open file -> Operation file ( Read operations 、 Write operations )-> Close file
2) Open file ( The underlying layer is called c Language )
open(file,mode='r')
a. The path of the file to be opened
Absolute path : The full path of the file in the computer
Relative paths : use . Represents the current directory ( The current directory refers to the directory of the file where the code of the open file is currently written )
use .. Represents the upper directory of the current directory
b.mode - File open mode ( Determines whether the supported operation is read or write after the file is opened ; Determines whether the data object of the operation is a data string or binary )
The value that determines how to open :r、w、a
r - read-only
w - Just write : After opening, it will delete and empty the contents of the original file
a - Just write : Add content after the original document
Determine the data type of the operation :t( The default value is )、b
t - The contents read and written to the file are of string type
b - What you read and what you write into the file are bytes( Binary system ) type
Be careful : When opening a file mode You must choose one of these two sets of values , If the value of the second group is not selected , Indicates the selected by default t
'''''''''
# f=open('./sources/aaa.txt','at')
# # f.read()
# f.write('lixingyu')
# f=open('./sources/aaa.txt','ab')
# # f.read()
# f.write(bytes('123456',encoding='utf-8'))
More secure sharing , Please pay attention to 【 Security info】 WeChat official account !
copyright notice
author[Wandering mage 12],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110607424446.html
The sidebar is recommended
- Introduction to the differences between Python and Java
- Explain Python CONDA in detail
- The pycham downloaded by MAC reports an error as soon as it is opened. The downloaded Python interpreter is also the latest version
- From entry to mastery, python full stack engineers have personally taught Python core technology and practical combat for ten years
- Python is used to detect some problems of word frequency in English text.
- How to choose between excel, database and pandas (Python third-party library)?
- WxPython download has been reporting errors
- Pyside6 UIC and other tools cannot be found in the higher version of pyside6 (QT for Python 6). How to solve it?
- About Python Crawlers
- Successfully imported pandas, unable to use dataframe
guess what you like
How to extract some keywords in the path with Python
Python encountered a problem reading the file!
When Python is packaged into exe, an error is reported when opening assertionerror: C: \ users \ Acer \ appdata \ local \ temp\_ MEI105682\distutils\core. pyc
Eight practical "no code" features of Python
Python meets SQL, so a useful Python third-party library appears
100 Python algorithm super detailed explanation: a hundred dollars and a hundred chickens
[fundamentals of Python] Python code and so on
When Python uses probit regression, the program statement is deleted by mistake, and then it appears_ raise_ linalgerror_ Unrecognized error of singular
Python testing Nicholas theorem
Accelerating parallel computing based on python (BL) 136
Random recommended
- Python dynamic programming (knapsack problem and longest common substring)
- Django uses queryset filter save, and an 'queryset' object has no attribute 'Save' error occurs. Solution?
- Analysis of built-in functions in Python learning
- Python office automation - 90 - file automation management - cleaning up duplicate files and batch modifying file names
- Python office automation - 91 - word file Automation - word operation and reading word files
- After python, go also runs smoothly on the browser
- Self taught Python 26 method
- Summary of Python Tkinter component function examples (code + effect picture) (RadioButton | button | entry | menu | text)
- Python implementation of official selection sorting of Luogu question list
- Application of Django template
- Get project root path and other paths in Python project
- Get, rename, and delete file names in Python projects
- How to set the width and height of Python operation table
- Python string preceded by 'f' R 'B' U '
- JSON and other types convert to each other in Python
- Key value of key combination in pynput in Python
- Conversion of Python PDF file to word file
- Interface testing uses Python decorators
- Get the current time in Python
- Python course notes -- Python string, detailed explanation of related functions
- Python file and folder operations
- Python file content operation
- Three basic data quality evaluation methods and python implementation
- Python data structure and mathematical algorithm examples (continuously updating)
- GUI interface mail sending program based on SMTP protocol server and python language
- Python application tool development: mail sending GUI program
- Python application tool development: PIP instruction GUI program
- Application development based on Python and MySQL database: student information score management system version 1.0
- [Python] sort and sorted
- [Python] create a two-dimensional array with list, which is easy to step on
- Multiply [email protected]
- About creating in Python project folder__ init__. Understanding of PY
- Python, zsbd
- Smplify introduction and in python2 7 operation in environment
- Boost(2):boost. Python library introduction and simple examples
- Boost (3): encapsulate C + + classes into Python classes
- Boost (5): extract the type of C + + language and extract class from Python object
- Boost(6):Boost. How Python converts C + + parameter and return value types
- Boost(7):Boost. Python encapsulates overloaded functions and passes default parameters
- Boost(8):Boost. Python implements Python objects and various types of built-in operations and methods