In python, how does one obtain the path of the script file?
First, we do not obtain the "current path", that is, OS. getcwd ()
This includes two obfuscation problems: first, obtaining the path of the current script file and second, obtaining the path of the script that start
# -*- coding:utf-8 -*-#遍历目录树import os,fnmatchdef all_files(root, patterns=‘*‘, single_level=False, yield_folder=False): # 将模式从字符串中取出放入列表中 patterns = patterns.split(‘;‘) for path, subdirs, files in os.walk(root): if yield_folder: files.extend(subdirs) files.sort() for fname in files: for pt in patterns: if fnmatch.fnmatch(fname, pt): yield os.path.join(path, fname) break if single_level: break# fnmatch 来检查文件
First, remove the current path:
Base_dir1 = Os.path.dirname (Os.path.dirname (__file__))
Base_dir2 = Os.path.dirname (Os.path.abspath (__file__))
# __file__: Refers to the directory address where this file resides (including the file name) but is related to the directory where the Python command is executed (relative address).
# dirname (__file__): The top level of the file.
# Abspath (__file__): The abs
path of the import module path needs to be viewed using Sys.path, as follows:ImportSYSPrint(Sys.path) above example Linux output: ["','/usr/lib/python2.7','/usr/lib/python2.7/plat-linux2','/USR/LIB/PYTHON2.7/LIB-TK','/usr/lib/python2.7/lib-old','/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages','/usr/lib/pymodules/python2.7'] The above instance Wind
One, the history of Python"Uncle Turtle" uses the C language, the use of a holiday written in high-level language.Language divided into: compiled interpretation typeC PythonTwo, the Python versionPYTHON2X and python3xManufacturers will update to 2020 continuous update (the following source code is basically the PYTHON3X interpreter mainly)Three, easy-to-use IDE toolsPycharm, supports Windows Authoring and L
!
Elif
A pair of if and else allows the code to execute two different results
But in development, you might want to perform multiple results in parallel, and you can use Elif
The syntax format is as follows:
if condition 1:condition 1 code that satisfies execution...elif Condition 2:code to execute when condition 2 is met...elif Condition 3:code to execute when condition 3 is met...Else:code that executes when none of the above conditions are metWhen multi-conditional
We chose to do Python programming under Ubuntu: Create a. py file, which starts with the Python code, and then uses the command $python file name in the shell to display the results of the run.Here are a couple of string-related functions and syntax:1. InputName = input ("What ' s your name?") Print "Hello," + name + "!"Input will be in the output "What ' s your
The path to learning in Python -2018/6/27
1. Multiple table operations add a record to add a many-to-many relationshipMethod One: Incoming author object= Book.objects.get(bid=1= Author.objects.get(name="gy"= Author.objects.get(name="yq")book.authors.add(gy, yq)Operation Result:Mode two: Incoming author primary key= Book.objects.get(bid=2)book.authors.add(34)Operation Result:To remove a many-to-many rela
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such this adding up all the values along the Path equals the given sum.For example:Given the below binary tree sum = 22 and, 5 / 4 8 / / / 4 / \ 7 2 1Return true, as there exist a root-to-leaf path
. (actually based on QueryPerformanceCounter () on WIN32, which is more accurate than milliseconds)Import Time Print (Time.clock ()) Time.sleep (2) Print (Time.clock ())View Code5.datetime ModuleShow Current TimeImport datetime Print (Datetime.datetime.now ())View CodeThird, random moduleThe random module mainly has the following functionsImportRandomPrint(Random.random ())#randomly generate a floating-point number less than 1Print(Random.randint (1,3))#randomly generates an integer that can be
classes in the data. - -Many, many more ... the the a total of 150 data samples the evenly distributed over 3 subspecies the 4 petals per sample, calyx shape Description - " " the the " " the 2 dividing the training set and the test set94 " " theX_train, X_test, y_train, y_test =train_test_split (Iris.data, the Iris.target, thetest_size=0.25,98Random_state=33) About - " "101 3 K Nearest Neighbor Classifier learning model and prediction102 " "103 #standardization of training data and test
quotes, when to use single quotes, when to use double quotes, three quotes S1 = ' yangchen ' s2 = "yangchen" msg = "My name is Coy, I ' m years old!" Print (msg)Three quotes are usually quoted in a long stringmsg = "" Today I want to write a poem, praise my deskmate, you see his black short hair, like a fried chicken. "Print (msg)2, String +*(1) +S1 = ' Coy ' s2 = ' ssss ' Print (S1+S2)(2) *S1= ' Coy ' Print (s1*8)Third, str index slicing and simple operation1, String index sliceThrough the ind
the space behind it is wasted.So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.There are a number of ways to create a generator. The first method is simple, as long as you change a list-gener
==commons.login) func () # and then we'll execute the following func, which means to execute the function inside the Commons else: print (" 404 ") # if Hasattr does not check, execute this sentence 404if __name__ = = ' __main__ ': # __name__ is the current module name and the module is named __main__ when the module is run directly. This means that when the module is run directly, the following code block will be run and the code block is not run when the module is imported.
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.