首页 > Python mkdir一个有难度的问题

Python mkdir一个有难度的问题

在编辑器里我写了os.mkdir(XXXXX)这里是绝对路径
那么我接下来的代码里该如何获取到这个路径,现在假装我看不见XXXX是什么了(眼睛瞎了)
用Python不用其他。


目测题主想表达运行程序的时候动态获取当前程序文件的绝对路径。

$ vim test.py

import os

current_path = os.path.abspath(os.path.curdir)
new_dir = 'XXX'
new_dir_path = os.path.join(current_path, new_dir)
os.mkdir(new_dir_path)

$ python test.py

用个变量不就完了?

path='/tmp/test'
os.mkdir(path)
print(path)

貌似题主的意思是想监听文件的事件
Linux上监听文件事件使用inotify。对inotify的简单介绍,摘录自baidu:
http://baike.baidu.com/link?url=q_BEqnf6L7kdjx2-kdEOADCl3iEZbvhW_Gx_WDh3Z4xSfZZJ0MQPNn6H6IVOHoBQHjglPZfaU5R68IqM9L57-a

Inotify 是一个 Linux 内核特性,它监控文件系统,并且及时向专门的应用程序发出相关的事件警告,比如删除、读、写和卸载操作等。您还可以跟踪活动的源头和目标等细节。
使用 inotify 很简单:创建一个文件描述符,附加一个或多个监视器(一个监视器 是一个路径和一组事件),然后使用 read 方法从描述符获取事件。read 并不会用光整个周期,它在事件发生之前是被阻塞的。
更好的是,因为 inotify 通过传统的文件描述符工作,您可以利用传统的 select 系统调用来被动地监控监视器和许多其他输入源。两种方法 — 阻塞文件描述符和使用 select— 都避免了繁忙轮询。

python对应的监听文件事件的lib为pyinotify ,由于mac上安装不了inotify,所以直接在网上找的代码,供题主参考;原连接为:http://www.tuicool.com/articles/fQ7bUr;
思路:通过pyinotify监听文件事件,通过轮询文件事件,来做相应的处理;按照生产者消费者的模型处理,生产者轮询文件事件,放进共享队列;消费者轮询消费共享队列中的数据。

inotify is not available on macosx-10.11-intel


import os
import datetime
import pyinotify
import logging
class MyEventHandler(pyinotify.ProcessEvent):
  logging.basicConfig(level=logging.INFO,filename='/var/log/monitor.log')
  #自定义写入那个文件,可以自己修改
  logging.info("Starting monitor...")
  
  def process_IN_ACCESS(self, event):
    print "ACCESS event:", event.pathname
  logging.info("ACCESS event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
  def process_IN_ATTRIB(self, event):
    print "ATTRIB event:", event.pathname
  logging.info("IN_ATTRIB event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
   def process_IN_CLOSE_NOWRITE(self, event):
    print "CLOSE_NOWRITE event:", event.pathname
    logging.info("CLOSE_NOWRITE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
   def process_IN_CLOSE_WRITE(self, event):
    print "CLOSE_WRITE event:", event.pathname
  logging.info("CLOSE_WRITE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
   def process_IN_CREATE(self, event):
    print "CREATE event:", event.pathname
  logging.info("CREATE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
   def process_IN_DELETE(self, event):
    print "DELETE event:", event.pathname
  logging.info("DELETE event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
   def process_IN_MODIFY(self, event):
    print "MODIFY event:", event.pathname
  logging.info("MODIFY event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
   def process_IN_OPEN(self, event):
    print "OPEN event:", event.pathname
  logging.info("OPEN event : %s  %s" % (os.path.join(event.path,event.name),datetime.datetime.now()))
  
  
def main():
  # watch manager
  wm = pyinotify.WatchManager()
  wm.add_watch('/tmp', pyinotify.ALL_EVENTS, rec=True)
  #/tmp是可以自己修改的监控的目录
  # event handler
  eh = MyEventHandler()

  # notifier
  notifier = pyinotify.Notifier(wm, eh)
  notifier.loop()

if __name__ == '__main__':
  main()
    
[root@centos6 monitor-folder]# python total-monitor.py
OPEN event: /tmp/.ICE-unix
CLOSE_NOWRITE event: /tmp/.ICE-unix
OPEN event: /tmp
CLOSE_NOWRITE event: /tmp
OPEN event: /tmp
CLOSE_NOWRITE event: /tmp
DELETE event: /tmp/aa
DELETE event: /tmp/adduser.conf
DELETE event: /tmp/adjtime
DELETE event: /tmp/aliases
DELETE event: /tmp/bash.bashrc
DELETE event: /tmp/bindresvport.blacklist
DELETE event: /tmp/environment
DELETE event: /tmp/fstab
DELETE event: /tmp/ipt.err
DELETE event: /tmp/ipt.out
DELETE event: /tmp/krb5.conf
DELETE event: /tmp/odbc.ini
DELETE event: /tmp/odbcinst.ini
DELETE event: /tmp/timezone
DELETE event: /tmp/ucf.conf
DELETE event: /tmp/warnquota.conf
DELETE event: /tmp/wgetrc
DELETE event: /tmp/xinetd.conf
CREATE event: /tmp/aa
OPEN event: /tmp/aa
ATTRIB event: /tmp/aa
CLOSE_WRITE event: /tmp/aa
CREATE event: /tmp/bb
OPEN event: /tmp/bb
ATTRIB event: /tmp/bb
CLOSE_WRITE event: /tmp/bb
CREATE event: /tmp/cc
OPEN event: /tmp/cc
ATTRIB event: /tmp/cc
CLOSE_WRITE event: /tmp/cc

上面是打印出来的监控状态,下面是我的操作代码:

[root@centos6 tmp]# ls
aa            bash.bashrc             ipt.err    odbcinst.ini    wgetrc
adduser.conf  bindresvport.blacklist  ipt.out    timezone        xinetd.conf
adjtime       environment             krb5.conf  ucf.conf
aliases       fstab                   odbc.ini   warnquota.conf
[root@centos6 tmp]# rm -rf *
[root@centos6 tmp]# touch aa
[root@centos6 tmp]# touch bb
[root@centos6 tmp]# touch cc
[root@centos6 tmp]# 

    
【热门文章】
【热门文章】