[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)

链接



: 陆续更新中...

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