Python is an interpreted language.

A python script goes through sourcecode  --> interpreter --> output.

Whereas languages, like C/C++, java,  goes through sourcecode --> compiler --> object code --> executor --> output

1. invoking the interpreter in 2 ways:

$ python

the python interpreter will be evoked.

$ python helloworld.py

the python script helloworld.py will be executed.

2. The interpreter and its encoding

Using the following statement. By default, Python source files are treated as encoded in ASCII

# -*- coding: encoding -*-
# -*- coding: cp1252 -*-

3. Keywords in python:

(32 keywords in python 2.7) if, elif, else, for, while, return, in, is, try, except, raise, break, continue,   not, and, or,   def, class,   import, from,   print, yield, pass,  exec, global, lambda, assert, as, del, finally, global, with

# the use of lambda
def make_increment(n):
return lambda x: x+n #lambda function # the use of print
print "Hello, World." # the use of yield
# the use of exec # the use of del
li = [2,4,6,67,"t", 12.4]
del li[0] #delete li[0]
del li #delete li

4. Built-in functions in python 2.7, totally 76 built-in functions

abs()
divmod()
input()
open()
staticmethod()
all()
enumerate()
int()
ord()
str()
any()
eval()
isinstance()
pow()
sum()
basestring()
execfile()
issubclass()
print()
super()
bin()
file()
iter()
property()
tuple()
bool()
#filter(function, iterable):
def func:
return x%3==0 or x%5==0
filter[f, range(2,25)]
#return a list
len( myvar ) return the length of myvar 
range():
bytearray() float() list() raw_input() unichr() callable() format() locals() reduce() unicode() chr() frozenset() long() reload() vars() classmethod() getattr()
# map()
def cube(x):
return x*x*x
map(cube, range(1,11))
# will generate a list of 10 element
repr()
xrange()
cmp()
globals()
max()
reversed()
zip()
compile()
hasattr()
memoryview()
round()
__import__()
complex()
hash()
min()
set()
delattr()
help()
next()
setattr()
dict()
hex()
object()
slice()
dir( myObj ) means show the information of myObj
id( myObj ) means return the address of myObj
oct()
sorted()

True, False (the value of a bool type )

5. Variables and expressions:

comments starts with a  # to the end of a line.

A variable should have a type, and use the built-in function type( myObj )  will tell you the type.

numeric operations are +, -, *, **, /, %

logical operators are and, or, not

    A new assignment makes an existing variable refer to a new value, and stop referring to the old value.

6. flow control and definition of function/class

# if statement

if <condition>:

<statement>

elif <condition>:

<statement>

else:

<statement>

# for statement

for <element> in <statement>:

<statement>

# while statement

while <condition>:

<statement>

# flow control

break    # break out of the innermost enclosing for/while loop

continue # continue with the next iteration of the loop

pass     # do nothing

# define a function

def <name> ( <parameter_list> ):

<statement>

# define a class

class <name>:

pass

# define a class inheriting from a parent class

class <name>(<base_class>):

pass

7. string, a built-in type, " xxx " or ' xxx '

single or double-quoted. The string is immutable.

fruit = "banana"
fruit[1] = "c" #Error
len(fruit) #the length of string
last_ch = fruit[-1] #the last char in the string
letter = fruit[2] #the 2nd char in string
slice_piece = fruit[0:3] #print [0,3) that is "ban"

8. list, [ ]

using [ ], the list is mutable which means we can modify/add/delete elements in the list.

list is a data type(data structure) in python, so it has some methods(operations), like append(), insert(), remove(), ...

# the following three squares are the same

squares = [x**2 for x in range(10)

squares = []

for x in range(10):

    squares.append(x**2)

    squares = map(lambda x:x**2, range(10))

(Actually, the element in the list is the pointer/reference)

li = [0] * 3    # li = [0,0,0]
a = "banana" # reference to a string
b = "banana" # reference to the same string
id(a) #135044008
id(b) #135044008 c = [1,2,3]
d = [1,2,3]
id(c) #reference to a list id = 3500264
id(d) #reference to another list id = 3428408
c = d #c and d refer to the same list id = 3428404. the list whose id=3500264 will be discarded. rst = []
rst.append(3) #rst = [3] #Passing a list as a parameter to a function passes a reference to the list, not a copy of the list
rst = myFunction(a)

9. Tuple, ( )

Tuple is immutable.

t1 = ('a',) #create a tuple with one single element, DO NOT miss that comma

    t2 = ('a') # t2 is a string, not a tuple

  a, b = b, a   #tuple assignment, the number of left variable should be the same as the number of right

10. Dictionary, {key : value , key2 : value2 }

key-value pairs.

The elements of a dictionary appear in a comma-separated list. key value key2 : value2 } Each element consists of a key-value pair.

Using immutable type as an index.

eng2number = {} # an empty dictionary
eng2number['one'] = 1 #
eng2number['two'] = 2 #
print eng2number # {'one':1,'two':2}
print eng2number['one'] # 1
delete eng2number['one'] #delete a element

11. set, { }

A set is an unordered collection with no duplicate elements. Using curly braces to create a set, or using set() to create a set from a list

basket = ['apple', 'orange', 'berry', 'orange', 'apple']    # a list

fruit = set(basket)    # fruit is a set initiated from a list, no duplicate in the fruit

fruit2 = {'apple', 'orange', 'berry', 'apple'}    # fruit2 = {'apple', 'orange', 'berry'}

fruit2.add("apple")    # add one element  

12.Summary:

slice operator [ ] 

operator+

assignment operator=

function parameter passing and return value

in operation

not in operation

14. Modules

from <module> import *

from <module> import <function_or_var_or_name>

import <function_or_var_or_name>

dir( <module> )   # see what's in the module

type( <module> )  # see the type of module

15. Class

 

  

    

1. get files in the current directory with the assum that the directory is like this:

a .py

|----dataFolder

|----Myfolder

|----1.csv , 2.csv, 3.csv

 # a.py
1 def getFileList():
file_list = []
cur_dir = os.getcwd()
for folder in os.walk(cur_dir).next()[1]:
f = os.path.join(cur_dir, folder)
for filename in os.walk(f).next()[1]:
file_path = os.path.join(f, filename)
file_list.append(file_path)
print "the total files are:\n", file_list
return file_list

in line 4, get all the folders in the cur_dir.

If want to get all files, in the cur_dir, than use:

for myfiles in os.walk(cur_dir).next()[2]:

2. get all files (only files not including folders) in one directory

os.listdir("some_dir")

[python] File path and system path的更多相关文章

  1. python内置模块[sys,os,os.path,stat]

    python内置模块[sys,os,os.path,stat] 内置模块是python自带功能,在使用内置模块时,需要遵循 先导入在 使用 一.sys 对象 描述 sys.argv 命令行参数获取,返 ...

  2. python os.walk()和os.path.walk()

    一.os.walk() 函数声明:os.walk(top,topdown=True,onerror=None) (1)参数top表示需要遍历的顶级目录的路径. (2)参数topdown的默认值是“Tr ...

  3. C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)

    文件流 FileStream  可读可写  大文件  释放 StreamReader 读取   释放 StreamWriter 写入   释放 using 中释放 File 可读可写  小文件 操作文 ...

  4. WebStorm failing to start with 'idea.system.path' error

    WebStorm failing to start with 'idea.system.path' error Ask Question up vote 2 down vote favorite   ...

  5. Python join() 方法与os.path.join()的区别

    Python join() 方法与os.path.join()的区别 pythonJoinos.path.join 今天工作中用到python的join方法,有点分不太清楚join() 方法与os.p ...

  6. exec: "docker-proxy": executable file not found in $PATH

    在执行 docker run 操作的时候,一直报如下错误: [root@etcd1 vagrant]# docker run --name redis-6379 -p 6379:6379 -d --r ...

  7. python脚本中selenium启动浏览器报错os.path.basename(self.path), self.start_error_message) selenium.common.excep

    在python脚本中,使用selenium启动浏览器报错,原因是未安装浏览器驱动,报错内容如下: # -*- coding:utf-8 -*-from selenium import webdrive ...

  8. Python os模块、os.path模块常用方法

    os模块:os模块在python中包含普遍的操作系统功能,下面列出了一些在os模块中比较有用的部分. os.sep 可以取代操作系统特定的路径分隔符.windows下为 "\" o ...

  9. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

随机推荐

  1. 新技术≠颠覆:CIO 要有战略耐心

    新技术≠颠覆:CIO 要有战略耐心 大数据,云时代,互联网思维, 物联网--最近一两年,这些字眼一次次地出现在各种大大小小的CIO会议上和他们的私下交流圈子里,作为对新技术最敏感的人群,一方面他们迫切 ...

  2. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

  3. 瞬间记住Javascript中apply与call的区别

    关于Javascript函数的apply与call方法的用法,网上的文章很多,我就不多话了.apply和call的作用很相似,但使用方式有区别 apply与call的第一个参数都是一个对象,这个对象就 ...

  4. 技术笔记:XMPP之openfire+spark+smack

    在即时通信这个领域目前只找到一个XMPP协议,在其协议基础上还是有许多成熟的产品,而且是开源的.所以还是想在这个领域多多了解一下. XMPP协议:具体的概念我就不写了,毕竟这东西网上到处是.简单的说就 ...

  5. JS or C#?不存在的脚本之争

    前言: 又来到了周末,小匹夫也终于有了喘口气写写博客的时间和精力.话说周五的下午,小匹夫偶然间晃了一眼蛮牛的QQ群,又看到了一个Unity3D开发中老生长谈的问题,“我的开发语言究竟是选择JavaSc ...

  6. 从Unity3D编译器升级聊起Mono

    接前篇Unity 5.3.5p8 C#编译器升级,本文侧重了解一些Mono的知识. Unity3D的编译器升级 新升级的Mono C#编译器(对应Mono 4.4) Unity编辑器及播放器所使用的M ...

  7. 说说DOM的那些事儿

    引子 先来一颗栗子: <img src="/sub/123.jpg" alt="test" /> <script type="tex ...

  8. Visual Studio Code预览版Ver 0.3.0试用体验

    当你开始阅读这篇文章时,请先不要把Visual Studio Code和.net.Windows联想到一起,因为VS Code是一个跨平台,支持30多种语言的开箱代码编辑器.不管你是.Net.Java ...

  9. Storm内部的消息传递机制

    作者:Jack47 转载请保留作者和原文出处 欢迎关注我的微信公众账号程序员杰克,两边的文章会同步,也可以添加我的RSS订阅源. 一个Storm拓扑,就是一个复杂的多阶段的流式计算.Storm中的组件 ...

  10. ASP.NET MVC Model元数据(三)

    ASP.NET MVC Model元数据(三) 前言 在上篇中我们大概的讲解了Model元数据的生成过程,并没有对Model元数据本身和详细的生成过程有所描述,本篇将会对详细的生成过程进行讲解,并且会 ...