| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1942 人关注过本帖
标题:文件目录操作
取消只看楼主 加入收藏
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
结帖率:40%
收藏
已结贴  问题点数:20 回复次数:4 
文件目录操作
#-*- coding: utf-8 -*-
from __future__ import with_statement
import platform
import os
import shutil
import stat
import ConfigParser


#check the type of system  
def CheckSys():
    sysname=platform.system()
    if sysname=="Windows":
        return "win"
    elif sysname=="Linux" :
        return "lin"
    else :
        return "other"
   
#check the type of path_filename   
def Check_file_or_dir(path_filename):
    #if not path_filename:
    #   path_filename=os.getcwd()
    exist = os.path.exists(path_filename)
    if not exist:
        print path_filename,"is not exist"
        return  
    if os.path.isfile(path_filename):  
        return "file"
    elif os.path.isdir(path_filename):
        return "dir"
    elif os.path.islink(path_filename):
        return "link"
    else:
        return "other"
#cd path
def CdDir(path):
    exist = os.path.exists(path)
    if not exist and not os.path.isdir(path):
        print path,"is not exist"
        return
    else:
        os.chdir(path)
   
#create the file or dir   
def CreateFileDir(path_filename,isdir):
    exist = os.path.exists(path_filename)
    if exist:
        print path_filename,"is Already exist"
        return   
    else:
        if isdir:
            os.mkdir(path_filename)
        else:
            f=open(path_filename,'w')
            f.close()
    return path_filename

#delete file or dir
def DeleteFileDir(path_filename):
    exist = os.path.exists(path_filename)
    filedir=Check_file_or_dir(path_filename)
    if exist :
        if filedir=="dir":
            shutil.rmtree(path_filename)
        elif filedir=="file":
            os.remove(path_filename)
        elif filedir=="link" :
            pass
    else:
        print path_filename,"is not Exist!"
        return

#copy the file or dir
def CopyFileDir(oldpath_filename,newpath_filename):
    exist = os.path.exists(oldpath_filename)
    filedir=Check_file_or_dir(oldpath_filename)
    if not exist:
        print oldpath_filename,"is not exist"
        return
    if filedir=="file":
        shutil.copyfile(oldpath_filename,newpath_filename)
    elif filedir=="dir" :
        CreateFileDir(newpath_filename,True)
        for filename in os.listdir(oldpath_filename):
            sourceFile = os.path.join(oldpath_filename,filename)
            targetFile = os.path.join(newpath_filename,filename)
            #cover the files
            if os.path.isfile(sourceFile):
                open(targetFile, "wb").write(open(sourceFile, "rb").read())
            else:
                CopyFileDir(sourceFile,targetFile)

#rename the file or dir               
def RenameFileDir(oldpath_filename,newpath_filename):
    exist = os.path.exists(oldpath_filename)
    if not exist:
        print oldpath_filename,"is not exist"
        return
    os.rename(oldpath_filename,newpath_filename)         

#list the files of file or dir      
def ListfileOfdir(path_filename,listfile):
    exist = os.path.exists(path_filename)
    if not exist :
        print path_filename,"is not exist"
        return  
    name=Check_file_or_dir(path_filename)
    if not path_filename:
        return
    else:
        if name=="file":
                listfile.append(path_filename)      
        elif name=="dir":
            for item in os.listdir(path_filename):
                itemsrc = os.path.join(path_filename, item)
                ListfileOfdir(itemsrc,listfile)
    return listfile

#change the mod of the file or dirAndfiles,but Record the old mode of the file or dirAndfile
def RecordChangeFileDirMode(path_filename,mode=0,modefile="FiledirMode.cfg"):
    exist = os.path.exists(path_filename)
    pathcfg= os.path.join(os.getcwd(), modefile)
    if not os.path.exists(pathcfg):
        f=open(pathcfg,'w')
        f.close()
    if not os.path.exists(path_filename):
        print path_filename,"is not exsit"
        return
    filedir=Check_file_or_dir(path_filename)
    config=ConfigParser.ConfigParser()
    with open(pathcfg,"w") as cfgfile:
        config.add_section("modeinfo")
        if filedir=="dir":
            for root, dirs, files in os.walk(path_filename):
                print root
                fileStats = os.stat (root)
                filemode=fileStats[stat.ST_MODE]           
                permiss=stat.S_IMODE(filemode)
                oldmode= oct(permiss)                     
                pathfilename=root.replace(':','?')
                config.set("modeinfo",pathfilename,oldmode)
                os.chmod(root, permiss & 0 )
                os.chmod(root, int(mode) )   
                for filename in files:
                    fp=os.path.join(root, filename)
                    fileStats = os.stat (fp)
                    filemode=fileStats[stat.ST_MODE]           
                    permiss=stat.S_IMODE(filemode)
                    oldmode= oct(permiss)                     
                    #status, output = commands.getstatusoutput('chmod 666 /home/chen')
                    #print oct(stat.S_IMODE(filestats[stat.ST_MODE]))
                    pathfilename=fp.replace(':','?')
                    config.set("modeinfo",pathfilename,oldmode)
                    os.chmod(fp, permiss & 0 )
                    os.chmod(fp, int(mode) )  
        elif filedir=="file":
            fileStats = os.stat (path_filename)
            filemode=fileStats[stat.ST_MODE]           
            permiss=stat.S_IMODE(filemode)
            oldmode= oct(permiss)
            pathfilename=path_filename.replace(':','?')
            config.set("modeinfo",pathfilename,oldmode)
            os.chmod(path_filename, permiss & 0 )
            os.chmod(path_filename, int(mode) )
        config.write(cfgfile)

#change the mode of the file or dirAndfiles,but don't Record the old mode of the file or dirAndfile   
def ChangeMod(path_filename,mode=0):
    if not os.path.exists(path_filename):
        print path_filename,"is not exsit"
        return
    filedir=Check_file_or_dir(path_filename)
    if filedir=="dir":
        for root, dirs, files in os.walk(path_filename):
            #print root
            rootstats=os.stat(root)
            os.chmod(root, stat.S_IMODE(rootstats[stat.ST_MODE]) & 0 )
            os.chmod(root, int(mode))  
            for filename in files:
                #print filename  
                fp=os.path.join(root, filename)
                #status, output = commands.getstatusoutput('chmod 666 /home/chen')
                filestats=os.stat(fp)
                #print oct(stat.S_IMODE(filestats[stat.ST_MODE]))
                os.chmod(fp, stat.S_IMODE(filestats[stat.ST_MODE]) & 0 )
                os.chmod(fp, int(mode))  
    elif filedir=="file":
        os.chmod(path_filename, stat.S_IMODE(filestats[stat.ST_MODE]) & 0 )
        os.chmod(path_filename, int(mode))

#only chang mode of the file or dir
def ChangefileMod(path_filename,mode=0):
    if not os.path.exists(path_filename):
        print path_filename,"is not exsit"
        return
    rootstats=os.stat(path_filename)
    os.chmod(path_filename, stat.S_IMODE(rootstats[stat.ST_MODE]) & 0 )
    os.chmod(path_filename, int(mode))

#read the old mode of the file or dir               
def ReadFileDirMode(path_filename,modefile="FiledirMode.cfg"):
    if not os.path.exists(path_filename):
        print path_filename,"is not exsit"
        return
    pathcfg= os.path.join(os.getcwd(), modefile)
    if not os.path.exists(pathcfg):
        print modefile,"is not exsit"
    config=ConfigParser.ConfigParser()
    with open(pathcfg,"r") as cfgfile:  
        config.readfp(cfgfile)
        pathfilename=path_filename.replace(':','?')
        mode=config.get("modeinfo",pathfilename)
        return mode
   
#read Current mode of the file or dir     
def ReadCurfiledirMode(path_filename):
    if not os.path.exists(path_filename):
        print path_filename,"is not exsit"
        return
    fileStats = os.stat (path_filename)
    filemode=fileStats[stat.ST_MODE]           
    permiss=stat.S_IMODE(filemode)
    curmode= oct(permiss)
    print curmode
    return curmode

#recover the mode of the file or dir
def RecoverFileDirMode(path_filename,modefile="FiledirMode.cfg"):
    mode=ReadFileDirMode(path_filename,modefile)
    print mode
    ChangefileMod(path_filename,mode)  
 
 
   
   





if __name__ == "__main__":
    #CreateFileDir("e:\\file2",True)
    #listfile=[]
    #ChangeMod(r"e:\file\f",r"e:\file\book")
    #CdDir(r"e:\file\book")
    #print os.getcwd()
    #RecordChangeFileDirMode(r"E:\test",0666)
    #print ReadFileDirMode(r"E:\test\hello\app.yaml")
    #myfile_stat = os.stat(r"E:\test\hello\app.yaml")  
    #size1 = myfile_stat[stat.ST_SIZE]  
    #mode1 = myfile_stat[stat.ST_MODE]   
    #print size1 ,mode1
    #print listfile
    ChangefileMod(r"E:\test\hello\app.yaml",0444)
    ReadCurfiledirMode(r"E:\test\hello\app.yaml")
   
    RecoverFileDirMode(r"E:\test\hello\app.yaml")
    ReadCurfiledirMode(r"E:\test\hello\app.yaml")
 
   

   
        
搜索更多相关主题的帖子: Windows system return future import 
2013-08-02 14:56
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
# -*- coding: UTF-8 -*-
'''
Created on 2013-8-9

@author: chenxiong
'''
import pymongo
dbdict={"host":"localhost","port":27017}
class TaskConnect(object):
    def __init__(self, host, port):
        #conn 类型<class 'pymongo.connection.Connection'>
        try:
            self.conn =pymongo.Connection(dbdict["host"], dbdict["port"])
        except  :
            print 'connect to %s:%s fail' %(host, port)
            exit(0)

    def __del__(self):
        self.conn.close()

    def use(self, dbname):
        # 这种[]获取方式同样适用于shell,下面的collection也一样
        #db 类型<class 'pymongo.database.Database'>
        self.db = self.conn[dbname]
 
    def setCollection(self, collectioname):
        if not self.db:
            print 'don\'t assign database'
            exit(0)
        else:
            self.coll = self.db[collectioname]
 
    def find(self, query = {}):
        #注意这里query是dict类型
        if type(query) is not dict:
            print 'the type of query isn\'t dict'
            exit(0)
        try:
            #result类型<class 'pymongo.cursor.Cursor'>
            if not self.coll:
                print 'don\'t assign collection'
            else:
                result = self.coll.find(query)
        except NameError:
            print 'some fields name are wrong in ',query
            exit(0)
        return result
 
    def insert(self, data):
        if type(data) is not dict:
            print 'the type of insert data isn\'t dict'
            exit(0)
        #insert会返回新插入数据的_id
        self.coll.insert(data)
 
    def remove(self, data):
        if type(data) is not dict:
            print 'the type of remove data isn\'t dict'
            exit(0)
            #remove无返回值
        self.coll.remove(data)
   
    def update(self, data, setdata):
        if type(data) is not dict or type(setdata) is not dict:
            print 'the type of update and data isn\'t dict'
            exit(0)
            #update无返回值
        self.coll.update(data,{'$set':setdata})
 
if __name__ == '__main__':
    connect = TaskConnect('localhost', 27017)
    connect.use('mydb')
    connect.setCollection('tasktable')
    connect.remove({'task':'ssss'})
    connect.insert({'task':"ssss", 'start_time':"hhhh","stop_time":"hshj","interval_time":"ssghh"})
   
    result = connect.find()
    connect.update({'task':"ssss"},{'task':'kkkkkkkkk'})
    #x也是dict类型,非常好
    for x in result:
        if 'task' in x:
            print x['_id'], x['task'], x['start_time'], x['stop_time']
        else:
            print x['_id'], x['a'], x['b']
   
2013-08-12 10:35
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
# -*- coding: UTF-8 -*-
'''
Created on 2013-8-12

@author: chenxiong
'''   
import Queue   
import threading   
import time   
   
class WorkManager(object):   
    def __init__(self,lock=None, work_num=1000,thread_num=2):
        #工作队列   
        self.work_queue = Queue.Queue()
        #线程队列   
        self.threads = []
        self.lock =lock  
        #初始化工作队列和线程队列   
        self.__init_work_queue(work_num)   
        self.__init_thread_pool(thread_num)
        
   
    """   
        初始化thread_num个线程 ,每个线程关联到同一个工组队列,放入线程队列中  
    """   
    def __init_thread_pool(self,thread_num):
        #创建 thread_num个线程放入线程队列   
        for i in range(thread_num):   
            self.threads.append(Work(self.work_queue,self.lock))   
   
    """   
        初始化工作队列,将工作放入到工组队列   
    """   
    def __init_work_queue(self, jobs_num):
        #将工作放入工作队列   
        for i in range(jobs_num):   
            self.add_job(do_job, i)  
 
   
    """   
        添加一项工作到工作队列  
    """   
    def add_job(self, func, *args):   
        self.work_queue.put((func, list(args)))#任务入队,Queue内部实现了同步机制
        
   
    """   
        等待所有线程运行完毕 ,判断线程是否激活状态是则调用  
    """      
    def wait_allcomplete(self):   
        for item in self.threads:   
            if item.isAlive():
                item.join()   
   
class Work(threading.Thread):   
    def __init__(self, work_queue,lock=None,threadname=None):   
        threading.Thread.__init__(self,threadname)   
        self.work_queue = work_queue
        self.lock=lock   
        self.start()   
   
    def run(self):   
        #死循环,从而让创建的线程在一定条件下关闭退出   
        while True:   
            try:
                self.lock.acquire()      
                do, args = self.work_queue.get(block=False)#任务异步出队,Queue内部实现了同步机制
                self.lock.release()   
                do(args)   
                self.work_queue.task_done()#通知系统任务完成   
            except:   
                break   
   
#具体要做的任务   
def do_job(args):   
    time.sleep(0.1)#模拟处理时间   
    print threading.current_thread(),"ssss"
   
def do_job1(args):
    for a in args:
        print a
   
if __name__ == '__main__':   
    start = time.time()
    lock=threading.Lock()   
    work_manager =  WorkManager(lock,10, 2)#或者work_manager =  WorkManager(10000, 20)
    work_manager.add_job(do_job1,{'1':"ss"},2,3)   
    work_manager.wait_allcomplete()   
    end = time.time()   
    print "cost all time: %s" % (end-start)  
2013-08-13 14:03
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
# -*- coding: UTF-8 -*-
'''
Created on 2013-8-8

@author: chenxiong
'''

#创建自己的线程类,必要时重写threading.Thread类的方法
import threading, time
import thread
from Queue import Queue
import random


#1、用 thread.start_new_thread() 创建线程   
def timer(no, interval):  
    cnt = 0  
    while cnt<10:  
        print 'Thread:(%d) Time:%s/n'%(no, time.ctime())  
        time.sleep(interval)  
        cnt+=1  
    thread.exit_thread()  
   
   
   
   
#2、线程共享数据时候要用线程锁   
count = 0
class Counter(threading.Thread):  
    def __init__(self, lock, threadName):  
        '''@summary: 初始化对象。
         
         @param lock: 琐对象。
          @param threadName: 线程名称。
        '''  
        super(Counter, self).__init__(name = threadName)  #注意:一定要显式的调用父类的初始  化函数。  
        self.lock = lock  
      
    def run(self):  
        '''@summary: 重写父类run方法,在线程启动后执行该方法内的代码。
        '''  
        global count  
        self.lock.acquire()  
        for i in xrange(10000):  
            count = count + 1
            print count   
        self.lock.release()
    def stop(self):  
        self.thread_stop = True

      



#3、线程通信(条件变量)共享product数据   
# 商品
product = None
# 条件变量
con = threading.Condition()
  
# 生产者方法
def produce():
    global product
     
    if con.acquire():
        while True :
            if product is None :
                print 'produce...'
                product = 'anything'
                 
                # 通知消费者,商品已经生产
                con.notify()
            
            # 等待通知
            con.wait()
            time.sleep( 2 )
    #con.release()
  
# 消费者方法
def consume():
    global product
     
    if con.acquire():
        while True :
            if product is not None :
                print 'consume...'
                product = None
                 
                # 通知生产者,商品已经没了
                con.notify()
            
            # 等待通知
            con.wait()
            time.sleep( 2 )
    #con.release()
  
 
 
  
#4、同步队列,一个队列的同步实现。
'''myqueue.put(10) 调用队列对象的put()方法在队尾插入一个项目。put()有两个参数,第一个item为必需的,为插入项目的值;第二个block

为可选参数,默认为1。如果队列当前为空且block为1,put()方法就使调用线程暂停,直到空出一个数据单元。如果block为0,put方法将引发Full

异常。

 将一个值从队列中取出myqueue.get() 调用队列对象的get()方法从队头删除并返回一个项目。可选参数为block,默认为1。如果队列为空且block

为1,get()就使调用线程暂停,直至有项目可用。如果block为0,队列将引发Empty异常。'''
# Producer thread

class Producer(threading.Thread):

    def __init__(self, threadname, queue):

        threading.Thread.__init__(self, name = threadname)

        self.sharedata = queue

    def run(self):

        for i in range(20):

            print self.getName(),'adding',i,'to queue'

            self.sharedata.put(i)

            time.sleep(random.randrange(10)/10.0)

            print self.getName(),'Finished'

 

# Consumer thread

class Consumer(threading.Thread):

    def __init__(self, threadname, queue):

        threading.Thread.__init__(self, name = threadname)

        self.sharedata = queue

    def run(self):

        for i in range(20):

            print self.getName(),'got a value:',self.sharedata.get()

            time.sleep(random.randrange(10)/10.0)

            print self.getName(),'Finished'

 
  

   
if __name__=='__main__':
    #1、用 thread.start_new_thread() 创建线程   
    thread.start_new_thread(timer, (1,1))   
    thread.start_new_thread(timer, (2,2))
   
     
   
    #2、线程共享数据count时候要用线程锁   
    lock = threading.Lock()  
    for i in range(5):
        #setDaemon(True)主线程退出子线程也将结束
        t= Counter(lock, "thread-" + str(i))
        t.setDaemon(True)   
        t.start()  
    time.sleep(2)   #确保线程都执行完毕
     
        
   
   
    #2、线程通信(条件变量)
    t2 = threading.Thread(target = consume)
    t1 = threading.Thread(target = produce)
    t2.start()
    t1.start()
   
   
   
   
    #3、同步队列
    queue = Queue()  
    producer = Producer('Producer', queue)
    consumer = Consumer('Consumer', queue)
    print 'Starting threads ...'
    producer.start()
    consumer.start()
    producer.join()
    consumer.join()

  

  
2013-08-14 10:25
sweet6hero
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2013-6-9
收藏
得分:0 
#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
    Author:cleverdeng
    E-mail:clverdeng@
"""

__version__ = '0.9'
__all__ = ["PinYin"]

import os.path


class PinYin(object):
    def __init__(self, dict_file='word.data'):
        self.word_dict = {}
        self.dict_file = dict_file


    def load_word(self):
        if not os.path.exists(self.dict_file):
            raise IOError("NotFoundFile")

        with file(self.dict_file) as f_obj:
            for f_line in f_obj.readlines():
                try:
                    line = f_line.split('    ')
                    self.word_dict[line[0]] = line[1]
                except:
                    line = f_line.split('   ')
                    self.word_dict[line[0]] = line[1]


    def hanzi2pinyin(self, string=""):
        result = []
        if not isinstance(string, unicode):
            string = string.decode("utf-8")

        
        for char in string:
            #print char
            key = '%X' % ord(char)#将汉字转为utf16编码,“码表”文件中用的是这个编码
            if  self.word_dict.get(key):
                result.append(self.word_dict.get(key, char).split()[0][:-1].lower())
            else:
                result.append(char)
        string=str()
        for s in result:
            string+=s
        return string


    def hanzi2pinyin_split(self, string="", split=""):
        result = self.hanzi2pinyin(string=string)
        if split == "":
            return result
        else:
            return split.join(result)


if __name__ == "__main__":
    test = PinYin()
    test.load_word()
    string = "diao 鱼 岛 是 组宋德福逛姐 去桑德 菲杰你 是谁"  
    print "out: %s" % test.hanzi2pinyin(string=string)

2013-08-16 11:42
快速回复:文件目录操作
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025678 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved