[python] python 常問問題集合



1、基本資料

1.1、指導文檔

Python 基礎教程
Python3 輸入和輸出


2、相互調用

2.1 bat

bat批處理執行python 的幾種方式 ———— 批處理, python打包成 exe文件

@echo off  
C:  
cd C:\Users\ldl\Desktop
start python test100.py 
start python 1.py 
start python 1.py 10
start python 1.py 100 
exit

怎麼讓bat執行完後不關閉,讓bat不關閉

pause

3、函數相關

3.1、向文件傳參

python腳本傳遞參數

[root@Test ~]# cat /opt/python.py 
#!/usr/local/bin/python
# -*- coding:utf-8 -*-

import sys

print(sys.argv[0])          #sys.argv[0] 類似於shell中的$0,但不是腳本名稱,而是腳本的路徑   
print(sys.argv[1])          #sys.argv[1] 表示傳入的第一個參數,既 hello

#運行結果:

[root@Test ~]# python /opt/python.py hello
/opt/python.py       #打印argv[0]  腳本路徑
hello                      #打印argv[1]  傳入的參數 hello

4、文件操作

4.1、文件夾拷貝

Copy directory contents into a directory with python [duplicate]

from distutils.dir_util import copy_tree

# copy subdirectory example
fromDirectory = "/a/b/c"
toDirectory = "/x/y/z"

copy_tree(fromDirectory, toDirectory)

4.2、查找文件

Find all files in a directory with extension .txt in Python

You can use glob:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)

or simply os.listdir:

import os
for file in os.listdir("/mydir"):
    if file.endswith(".txt"):
        print(os.path.join("/mydir", file))

or if you want to traverse directory, use os.walk:

import os
for root, dirs, files in os.walk("/mydir"):
    for file in files:
        if file.endswith(".txt"):
             print(os.path.join(root, file))

5、數據結構

5.1 SET

Get unique values from a list in python [duplicate]

mylist = ['nowplaying', 'PBS', 'PBS', 'nowplaying', 'job', 'debate', 'thenandnow']
myset = set(mylist)
print(myset)

鏈接



: 陸續更新中...

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章