zoukankan      html  css  js  c++  java
  • 每天学点python(3)

    ========================================         目录        =====================================================

    1. 输入输出:.repr(), str(), str.ljust(), str.centre(), str.rjust(), zfill(), str.formate(), !s, !r, :, [], **, %(similar with c)

    2. 文件读写:f.readline() f.readlines() f.write() f.tell()

    3. pickle类:pickle.dump(x, f) x = pickle.load(f)

    4. 异常

    5. 抛出异常

    6. finally

    7. 类

    9. 函数参数 *args 和 **kwargs

    10. 文件操作小结

    =======================================================================================================

    1. 输入输出

     函数 str() 用于将值转化为适于人阅读的形式,而 repr() 转化为供解释器读取的形式 函数 str() 用于将值转化为适于人阅读的形式,而 repr() 转化为供解释器读取的形式。根据我的理解,str()函数相当于java的toString().

    >>> s = 'Hello, world.'
    >>> str(s)
    'Hello, world.'
    >>> repr(s)
    "'Hello, world.'"
    >>> str(1.0/7.0)
    '0.142857142857'
    >>> repr(1.0/7.0)
    '0.14285714285714285'
    >>> x = 10 * 3.25
    >>> y = 200 * 200
    >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...'
    >>> print s
    The value of x is 32.5, and y is 40000...
    >>> # The repr() of a string adds string quotes and backslashes:
    ... hello = 'hello, world\n'
    >>> hellos = repr(hello)
    >>> print hellos
    'hello, world\n'
    >>> # The argument to repr() may be any Python object:
    ... repr((x, y, ('spam', 'eggs')))
    "(32.5, 40000, ('spam', 'eggs'))"
    >>> for x in range(1, 11):
    ...     print repr(x).rjust(2), repr(x*x).rjust(3),
    ...     # Note trailing comma on previous line
    ...     print repr(x*x*x).rjust(4)
    ...
     1   1    1
     2   4    8
     3   9   27
     4  16   64
     5  25  125
     6  36  216
     7  49  343
     8  64  512
     9  81  729
    10 100 1000
    
    >>> for x in range(1,11):
    ...     print '{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)
    ...
     1   1    1
     2   4    8
     3   9   27
     4  16   64
     5  25  125
     6  36  216
     7  49  343
     8  64  512
     9  81  729
    10 100 1000

    Note that in the first example, one space between each column was added by the way print works: it always adds spaces between its arguments.

    str.ljust() and str.centre and str.rjust()三个函数。这些函数只是输出新的字符串,并不改变什么。如果输出的字符串太长,它们也不会截断它,而是原样输出,这会使你的输出格式变得混乱,不过总强过另一种选择(截断字符串),因为那样会产生错误的输出值。(如果你确实需要截断它,可以使用切割操作,例如: x.ljust( n)[:n] 。

    zfill() 它用于向数值的字符串表达左侧填充 0。该函数可以正确理解正负号

    >>> '12'.zfill(5)
    '00012'
    >>> '-3.14'.zfill(7)
    '-003.14'
    >>> '3.14159265359'.zfill(5)
    '3.14159265359'

    str.format()的用法:

    >>> print 'We are the {} who say "{}!"'.format('knights', 'Ni')
    We are the knights who say "Ni!"

    还有

    >>> print '{0} and {1}'.format('spam', 'eggs')
    spam and eggs
    >>> print '{1} and {0}'.format('spam', 'eggs')
    eggs and spam

    >>> print 'This {food} is {adjective}.'.format(
    ...       food='spam', adjective='absolutely horrible')
    This spam is absolutely horrible.

    最后

    >>> print 'The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred',
    ...                                                    other='Georg')
    The story of Bill, Manfred, and Georg.

    '!s' (apply str()) and '!r' (apply repr()) can be used to convert the value before it is formatted.

    >>> import math
    >>> print 'The value of PI is approximately {}.'.format(math.pi)
    The value of PI is approximately 3.14159265359.
    >>> print 'The value of PI is approximately {!r}.'.format(math.pi)
    The value of PI is approximately 3.141592653589793.

    An optional ':' and format specifier can follow the field name. This allows greater control over how the value is formatted. The following example rounds Pi to three places after the decimal.

    >>> import math
    >>> print 'The value of PI is approximately {0:.3f}.'.format(math.pi)
    The value of PI is approximately 3.142.

    在字段后的 ':' 后面加一个整数会限定该字段的最小宽度,这在美化表格时很有用。

    >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
    >>> for name, phone in table.items():
    ...     print '{0:10} ==> {1:10d}'.format(name, phone)
    ...
    Jack       ==>       4098
    Dcab       ==>       7678
    Sjoerd     ==>       4127

    如果你有个实在是很长的格式化字符串,不想分割它。如果你可以用命名来引用被格式化的变量而不是位置就好了。有个简单的方法,可以传入一个字典,用中括号访问它的键

    >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
    >>> print ('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; '
    ...        'Dcab: {0[Dcab]:d}'.format(table))
    Jack: 4098; Sjoerd: 4127; Dcab: 8637678

    或者

    >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
    >>> print 'Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)
    Jack: 4098; Sjoerd: 4127; Dcab: 8637678

    这种方式与新的内置函数 vars() 组合使用非常有效。该函数返回包含所有局部变量的字典。 要进一步了解字符串格式化方法 str.format() ,参见 formatstrings 。

    操作符 % 也可以用于字符串格式化。它以类似 sprintf() 的方式解析左参数,将右参数应用于此,得到格式化操作生成的字符串,例如

    >>> import math
    >>> print 'The value of PI is approximately %5.3f.' % math.pi
    The value of PI is approximately 3.142.

    2. 文件读写

    >>> f = open('/tmp/workfile', 'w')  # 
    >>> print f
    <open file '/tmp/workfile', mode 'w' at 80a0960>

     'r' ,此选项使文件只读;模式 参数是可选的。如果没有指定,默认为 'r' 模式。

     'w' ,此选项使文件只写(对于同名文件,该操作使原有文件被覆盖);

     'a',此选项以追加方式打开文件; 

     'r+' ,此选项以读写方式打开文件; 

    值得注意的是:在Windows 平台上, 'b' 模式以二进制方式打开文件,所以可能会有类似于 'rb' , 'wb' , 'r+b' 等等模式组合。Windows 平台上文本文件与二进制文件是有区别的,读写文本文件时,行尾会自动添加行结束符。这种后台操作方式对 ASCII 文本文件没有什么问题,但是操作 JPEG 或 .EXE这样的二进制文件时就会产生破坏。在操作这些文件时一定要记得以二进制模式打开。在 Unix 上,加一个 'b' 模式也一样是无害的,所以你可以一切二进制文件处理中平台无关的使用它。

    ==================

    要读取文件内容,需要调用 f.read(size) ,该方法读取若干数量的数据并以字符串形式返回其内容, size 是可选的数值,指定字符串长度。如果没有指定 size或者指定为负数,就会读取并返回整个文件。当文件大小为当前机器内存两倍时,就会产生问题。反之,会尽可能按比较大的 size 读取和返回数据。如果到了文件末尾,f.read()会返回一个空字符串(”“)。

    >>> f.read()
    'This is the entire file.\n'
    >>> f.read()
    ''

    f.readline() 从文件中读取单独一行,字符串结尾会自动加上一个换行符(\ n ),只有当文件最后一行没有以换行符结尾时,这一操作才会被忽略。这样返回值就不会有混淆,如果如果 f.readline() 返回一个空字符串,那就表示到达了文件末尾,如果是一个空行,就会描述为 \n ,一个只包含换行符的字符串。

    >>> f.readline()
    'This is the first line of the file.\n'
    >>> f.readline()
    'Second line of the file\n'
    >>> f.readline()
    ''

    f.readlines()返回一个列表,其中包含了文件中所有的数据行。如果给定了 sizehint 参数,就会读入多于一行的比特数,从中返回多行文本。这个功能通常用于高效读取大型行文件,避免了将整个文件读入内存。这种操作只返回完整的行。

    >>> f.readlines()
    ['This is the first line of the file.\n', 'Second line of the file\n']

    有个按行读取的好办法是在文件对象上循环

    >>> for line in f:
            print line,
    
    This is the first line of the file.
    Second line of the file

    f.write(string) writes the contents of string to the file, returning None.

    如果需要写入字符串以外的数据,就要先把这些数据转换为字符串。

    >>> value = ('the answer', 42)
    >>> s = str(value)
    >>> f.write(s)

    f.tell() returns an integer giving the file object’s current position in the file, measured in bytes from the beginning of the file.from_what 值为 0 表示自文件起始处开始,1 表示自当前文件指针位置开始,2 表示自文件末尾开始。 from_what 可以忽略,其默认值为零,此时从文件头开始。

    >>> f = open('/tmp/workfile', 'r+')
    >>> f.write('0123456789abcdef')
    >>> f.seek(5)     # Go to the 6th byte in the file
    >>> f.read(1)
    '5'
    >>> f.seek(-3, 2) # Go to the 3rd byte before the end
    >>> f.read(1)
    'd'

    用关键字 with 处理文件对象是个好习惯。它的先进之处在于文件用完后会自动关闭,就算发生异常也没关系。它是try-finally 块的简写。

    >>> with open('/tmp/workfile', 'r') as f:
    ...     read_data = f.read()
    >>> f.closed
    True

    3. pickle 模块

    pickle 是存储 Python 对象以供其它程序或其本身以后调用的标准方法。几乎可以把任何 Python 对象 (甚至是一些 Python 代码段!)表达为为字符串,这一过程称之为封装 ( pickling )。

    If you have an object x, and a file object f that’s been opened for writing, the simplest way to pickle the object takes only one line of code:

    pickle.dump(x, f)

    To unpickle the object again, if f is a file object which has been opened for reading:

    x = pickle.load(f)

    4. 异常

    >>> while True:
    ...     try:
    ...         x = int(raw_input("Please enter a number: "))
    ...         break
    ...     except ValueError:
    ...         print "Oops!  That was no valid number.  Try again..."
    ...

    一个except子句可以在括号中列出多个异常的名字

    ... except (RuntimeError, TypeError, NameError):
    ...     pass

    最后一个 except 子句可以省略异常名,把它当做一个通配项使用。一定要慎用这种方法,因为它很可能会屏蔽掉真正的程序错误,使人无法发现!

    import sys
    
    try:
        f = open('myfile.txt')
        s = f.readline()
        i = int(s.strip())
    except IOError as e:
        print "I/O error({0}): {1}".format(e.errno, e.strerror)
    except ValueError:
        print "Could not convert data to an integer."
    except:
        print "Unexpected error:", sys.exc_info()[0]
        raise

    try ... except 语句可以带有一个 else 子句 ,该子句只能出现在所有 except 子句之后。当 try 语句没有抛出异常时,需要执行一些代码,可以使用这个子句。

    for arg in sys.argv[1:]:
        try:
            f = open(arg, 'r')
        except IOError:
            print 'cannot open', arg
        else:
            print arg, 'has', len(f.readlines()), 'lines'
            f.close()

    使用 else 子句比在 try 子句中附加代码要好,因为这样可以避免 try ... except 意外的截获本来不属于它们保护的那些代码抛出的异常。

    更好的做法是给异常传递一个参数(如果要传递多个参数,可以传递一个元组),把它绑定到 message 属性。一旦异常发生,它会在抛出前绑定所有指定的属性。

    >>> try:
    ...    raise Exception('spam', 'eggs')
    ... except Exception as inst:
    ...    print type(inst)     # the exception instance
    ...    print inst.args      # arguments stored in .args
    ...    print inst           # __str__ allows args to printed directly
    ...    x, y = inst.args
    ...    print 'x =', x
    ...    print 'y =', y
    ...
    <type 'exceptions.Exception'>
    ('spam', 'eggs')
    ('spam', 'eggs')
    x = spam
    y = eggs
     

    异常处理句柄不止可以处理直接发生在 try 子句中的异常,即使是其中(甚至是间接)调用的函数,发生了异常,也一样可以处理。

    >>> def this_fails():
    ...     x = 1/0
    ...
    >>> try:
    ...     this_fails()
    ... except ZeroDivisionError as detail:
    ...     print 'Handling run-time error:', detail
    ...
    Handling run-time error: integer division or modulo by zero

    5. 抛出异常

    可以用 raise 语句强制指定的异常发生

    >>> raise NameError('HiThere')
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    NameError: HiThere

    要抛出的异常由 raise 的唯一参数标识。它必需是一个异常实例或异常类(继承自 Exception 的类)。 如果你需要明确一个异常是否抛出,但不想处理它, raise 语句可以让你很简单的重新抛出该异常。

    >>> try:
    ...     raise NameError('HiThere')
    ... except NameError:
    ...     print 'An exception flew by!'
    ...     raise
    ...
    An exception flew by!
    Traceback (most recent call last):
      File "<stdin>", line 2, in ?
    NameError: HiThere

    5. 自定义异常

    在程序中可以通过创建新的异常类型来命名自己的异常(Python 类的内容请参见 Classes 类 )。异常类通常应该直接或间接的从 Exception 类派生

    >>> class MyError(Exception):
    ...     def __init__(self, value):
    ...         self.value = value
    ...     def __str__(self):
    ...         return repr(self.value)
    ...
    >>> try:
    ...     raise MyError(2*2)
    ... except MyError as e:
    ...     print 'My exception occurred, value:', e.value
    ...
    My exception occurred, value: 4
    >>> raise MyError('oops!')
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    __main__.MyError: 'oops!'

    In this example, the default __init__() of Exception has been overridden. The new behavior simply creates the value attribute. This replaces the default behavior of creating the args attribute.

    新的方式简单的创建 value 属性。这就替换了原来创建args 属性的方式。 异常类中可以定义任何其它类中可以定义的东西,但是通常为了保持简单,只在其中加入几个属性信息,以供异常处理句柄提取。如果一个新创建的模块中需要抛出几种不同的错误时,一个通常的作法是为该模块定义一个异常基类,然后针对不同的错误类型派生出对应的异常子类。

    class Error(Exception):
        """Base class for exceptions in this module."""
        pass
    
    class InputError(Error):
        """Exception raised for errors in the input.
    
        Attributes:
            expr -- input expression in which the error occurred
            msg  -- explanation of the error
        """
    
        def __init__(self, expr, msg):
            self.expr = expr
            self.msg = msg
    
    class TransitionError(Error):
        """Raised when an operation attempts a state transition that's not
        allowed.
    
        Attributes:
            prev -- state at beginning of transition
            next -- attempted new state
            msg  -- explanation of why the specific transition is not allowed
        """
    
        def __init__(self, prev, next, msg):
            self.prev = prev
            self.next = next
            self.msg = msg

    6. finally

    >>> try:
    ...     raise KeyboardInterrupt
    ... finally:
    ...     print 'Goodbye, world!'
    ...
    Goodbye, world!
    KeyboardInterrupt

    还有一个例子

    >>> def divide(x, y):
    ...     try:
    ...         result = x / y
    ...     except ZeroDivisionError:
    ...         print "division by zero!"
    ...     else:
    ...         print "result is", result
    ...     finally:
    ...         print "executing finally clause"
    ...
    >>> divide(2, 1)
    result is 2
    executing finally clause
    >>> divide(2, 0)
    division by zero!
    executing finally clause
    >>> divide("2", "1")
    executing finally clause
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      File "<stdin>", line 3, in divide
    TypeError: unsupported operand type(s) for /: 'str' and 'str'

     7. 类

    创建类:

    class MyClass:
        """A simple example class"""
        i = 12345
        def f(self):
            return 'hello world'

    可以直接调用类的成员变量和属性:

    MyClass.i
    MyClass.f
    MyClass.__doc__

    类实例化:

    x = MyClass()

    创建类的实例的时候,会调用__init__()方法,类似于java和c++里的构造方法:

    def __init__(self):
        self.data = []

    通过下面这个例子可以理解:

    >>> class Complex:
    ...     def __init__(self, realpart, imagpart):
    ...         self.r = realpart
    ...         self.i = imagpart
    ...
    >>> x = Complex(3.0, -4.5)
    >>> x.r, x.i
    (3.0, -4.5)

    x.f是一个方法,但是不是一个函数

    MyClass.f是一个函数

    调用方法,也可以这样:

    xf = x.f
    while True:
        print xf()

    不得不说,python真是太随意了。。。

    调用x.f()的时候,等于调用MyClass.f(x),好绕。。。

    以 n 个参数的列表去调用一个方法就相当于将方法的对象插入到参数列表的最前面后,以这个列表去调用相应的函数。

    下面这段还没理解,不过目前还用不到,暂且放在这里:

    What exactly happens when a method is called? You may have noticed that x.f() was called without an argument above, even though the function definition for f() specified an argument. What happened to the argument? Surely Python raises an exception when a function that requires an argument is called without any — even if the argument isn’t actually used...

    Actually, you may have guessed the answer: the special thing about methods is that the object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x). In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s object before the first argument.

    If you still don’t understand how methods work, a look at the implementation can perhaps clarify matters. When an instance attribute is referenced that isn’t a data attribute, its class is searched. If the name denotes a valid class attribute that is a function object, a method object is created by packing (pointers to) the instance object and the function object just found together in an abstract object: this is the method object. When the method object is called with an argument list, a new argument list is constructed from the instance object and the argument list, and the function object is called with this new argument list.

     这里有个完整的例子

    class Character:
        def __init__(self, name, initial_health):
            self.name = name
            self.health = initial_health
            self.inventory = []
            
        def __str__(self):
            s  = "Name: " + self.name
            s += " Health: " + str(self.health)
            s += " Inventory: " + str(self.inventory)
            return s
        
        def grab(self, item):
            self.inventory.append(item)
            
        def get_health(self):
            return self.health
        
    def example():
        me = Character("Bob", 20)
        print str(me)
        me.grab("pencil")
        me.grab("paper")
        print str(me)
        print "Health:", me.get_health()
        
    example()

     8. 

    查看一个变量的类型,可以用

    >>>a = 1
    >>>print a.__class__
    <type 'int'>

     查看一个对象可以调用的方法,可以用

    >>>a = [1, 2, 3]
    >>>dir(a)
    ['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']

    9. 函数参数 *args 和 **kwargs

    这样设计是为了传递多个参数,*args是non-keyworded,   **kwargs是keyworded。通过定义和调用函数两个场景解释。

    定义函数的时候:参数是个正常的参数,一个*args

    def test_var_args(farg, *args):
        print "formal arg:", farg
        for arg in args:
            print "another arg:", arg
    
    test_var_args(1, "two", 3)

    formal arg: 1
    another arg: two
    another arg: 3
     

    keyworded:

    def test_var_kwargs(farg, **kwargs):
        print "formal arg:", farg
        for key in kwargs:
            print "another keyword arg: %s: %s" % (key, kwargs[key])
    
    test_var_kwargs(farg=1, myarg2="two", myarg3=3)


    formal arg: 1
    another keyword arg: myarg2: two
    another keyword arg: myarg3: 3

    调用函数的时候:

    def test_var_args_call(arg1, arg2, arg3):
        print "arg1:", arg1
        print "arg2:", arg2
        print "arg3:", arg3
    
    args = ("two", 3)
    test_var_args_call(1, *args)

    ===================================
    arg1: 1
    arg2: two
    arg3: 3
    def test_var_args_call(arg1, arg2, arg3):
        print "arg1:", arg1
        print "arg2:", arg2
        print "arg3:", arg3
    
    kwargs = {"arg3": 3, "arg2": "two"}
    test_var_args_call(1, **kwargs)

    ====================================

    arg1: 1
    arg2: two
    arg3: 3

     10. 文件操作

    python中对文件、文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块。

    得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()

    返回指定目录下的所有文件和目录名:os.listdir()

    函数用来删除一个文件:os.remove()

    删除多个目录:os.removedirs(r“c:\python”)

    检验给出的路径是否是一个文件:os.path.isfile()

    检验给出的路径是否是一个目录:os.path.isdir()

    判断是否是绝对路径:os.path.isabs()

    检验给出的路径是否真地存:os.path.exists()

    返回一个路径的目录名和文件名:os.path.split()     eg os.path.split('/home/swaroop/byte/code/poem.txt') 结果:('/home/swaroop/byte/code', 'poem.txt') 

    分离扩展名:os.path.splitext()

    获取路径名:os.path.dirname()

    获取文件名:os.path.basename()

    运行shell命令: os.system()

    读取和设置环境变量:os.getenv() 与os.putenv()

    给出当前平台使用的行终止符:os.linesep    Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'

    指示你正在使用的平台:os.name       对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix'

    重命名:os.rename(old, new)

    创建多级目录:os.makedirs(r“c:\python\test”)

    创建单个目录:os.mkdir(“test”)

    获取文件属性:os.stat(file)

    修改文件权限与时间戳:os.chmod(file)

    终止当前进程:os.exit()

    获取文件大小:os.path.getsize(filename)


    文件操作:
    os.mknod("test.txt")        创建空文件
    fp = open("test.txt",w)     直接打开一个文件,如果文件不存在则创建文件

    关于open 模式:

    w     以写方式打开,
    a     以追加模式打开 (从 EOF 开始, 必要时创建新文件)
    r+     以读写模式打开
    w+     以读写模式打开 (参见 w )
    a+     以读写模式打开 (参见 a )
    rb     以二进制读模式打开
    wb     以二进制写模式打开 (参见 w )
    ab     以二进制追加模式打开 (参见 a )
    rb+    以二进制读写模式打开 (参见 r+ )
    wb+    以二进制读写模式打开 (参见 w+ )
    ab+    以二进制读写模式打开 (参见 a+ )

    fp.read([size])                     #size为读取的长度,以byte为单位

    fp.readline([size])                 #读一行,如果定义了size,有可能返回的只是一行的一部分

    fp.readlines([size])                #把文件每一行作为一个list的一个成员,并返回这个list。其实它的内部是通过循环调用readline()来实现的。如果提供size参数,size是表示读取内容的总长,也就是说可能只读到文件的一部分。

    fp.write(str)                      #把str写到文件中,write()并不会在str后加上一个换行符

    fp.writelines(seq)            #把seq的内容全部写到文件中(多行一次性写入)。这个函数也只是忠实地写入,不会在每行后面加上任何东西。

    fp.close()                        #关闭文件。python会在一个文件不用后自动关闭文件,不过这一功能没有保证,最好还是养成自己关闭的习惯。  如果一个文件在关闭后还对其进行操作会产生ValueError

    fp.flush()                                      #把缓冲区的内容写入硬盘

    fp.fileno()                                      #返回一个长整型的”文件标签“

    fp.isatty()                                      #文件是否是一个终端设备文件(unix系统中的)

    fp.tell()                                         #返回文件操作标记的当前位置,以文件的开头为原点

    fp.next()                                       #返回下一行,并将文件操作标记位移到下一行。把一个file用于for … in file这样的语句时,就是调用next()函数来实现遍历的。

    fp.seek(offset[,whence])              #将文件打操作标记移到offset的位置。这个offset一般是相对于文件的开头来计算的,一般为正数。但如果提供了whence参数就不一定了,whence可以为0表示从头开始计算,1表示以当前位置为原点计算。2表示以文件末尾为原点进行计算。需要注意,如果文件以a或a+的模式打开,每次进行写操作时,文件操作标记会自动返回到文件末尾。

    fp.truncate([size])                       #把文件裁成规定的大小,默认的是裁到当前文件操作标记的位置。如果size比文件的大小还要大,依据系统的不同可能是不改变文件,也可能是用0把文件补到相应的大小,也可能是以一些随机的内容加上去。

    目录操作:
    os.mkdir("file")                   创建目录
    复制文件:
    shutil.copyfile("oldfile","newfile")       oldfile和newfile都只能是文件
    shutil.copy("oldfile","newfile")            oldfile只能是文件夹,newfile可以是文件,也可以是目标目录
    复制文件夹:
    shutil.copytree("olddir","newdir")        olddir和newdir都只能是目录,且newdir必须不存在
    重命名文件(目录)
    os.rename("oldname","newname")       文件或目录都是使用这条命令
    移动文件(目录)
    shutil.move("oldpos","newpos")   
    删除文件
    os.remove("file")
    删除目录
    os.rmdir("dir")只能删除空目录
    shutil.rmtree("dir")    空目录、有内容的目录都可以删
    转换目录
    os.chdir("path")   换路径

     1 将文件夹下所有图片名称加上'_fc'
    
    python代码:
    
    # -*- coding:utf-8 -*-
    import re
    import os
    import time
    #str.split(string)分割字符串
    #'连接符'.join(list) 将列表组成字符串
    def change_name(path):
        global i
        if not os.path.isdir(path) and not os.path.isfile(path):
            return False
        if os.path.isfile(path):
            file_path = os.path.split(path) #分割出目录与文件
            lists = file_path[1].split('.') #分割出文件与文件扩展名
            file_ext = lists[-1] #取出后缀名(列表切片操作)
            img_ext = ['bmp','jpeg','gif','psd','png','jpg']
            if file_ext in img_ext:
                os.rename(path,file_path[0]+'/'+lists[0]+'_fc.'+file_ext)
                i+=1 #注意这里的i是一个陷阱
            #或者
            #img_ext = 'bmp|jpeg|gif|psd|png|jpg'
            #if file_ext in img_ext:
            #    print('ok---'+file_ext)
        elif os.path.isdir(path):
            for x in os.listdir(path):
                change_name(os.path.join(path,x)) #os.path.join()在路径处理上很有用
    
    
    img_dir = 'D:\\xx\\xx\\images'
    img_dir = img_dir.replace('\\','/')
    start = time.time()
    i = 0
    change_name(img_dir)
    c = time.time() - start
    print('程序运行耗时:%0.2f'%(c))
    print('总共处理了 %s 张图片'%(i))
    
    输出结果:
    
    程序运行耗时:0.11
    总共处理了 109 张图片
  • 相关阅读:
    CSS定位(Position)
    深入理解Javascript闭包(二)
    深入理解Javascript闭包(一)
    如何将页脚固定在页面底部
    JQuery中$.fn的用法示例
    关于HttpWebRequest.KeepAlive
    Fiddler 教程
    js数组的操作
    oracle 修改字段类型的方法(转)
    Oracle查看表结构的几种方法(转后加工)
  • 原文地址:https://www.cnblogs.com/dollarzhaole/p/2778946.html
Copyright © 2011-2022 走看看