Exemple #1
0
def parseFiles():
    if not os.path.exists(path):
        os.makedirs(path)
    for each in elements:
        tosave = path + os.sep() + each.get_text().replace(" ", "_")
        with open(tosave, 'wb') as f:
            f.write(requests.get(source + each.get('href')).content)
def write_image_to_tfrecords():
    # TFRcord生成器
    writer = tf.python_io.TFRecordWriter(os.path.join(data_dir,
                                                      train_set_name))

    # fpath_list = os.listdir(os.path.join(data_dir, 'train'))
    # 当目录中存在多级文件夹时,使用os.walk
    for root, dirs, fnames in os.walk("dataset/train"):
        # 遍历每一个图像
        for fname in fnames:
            # 图像数据
            """
            cv2.imread(fpath, CV_LOAD_IMAGE_ANYDEPTH)或者cv2.imread(fpath, -1) 保持原深度
            cv2.imread(fpath, CV_LOAD_IMAGE_COLOR)或者1,强制转换成彩色图像,即3通道
            cv2.imread(fpath, CV_LOAD_IMAGE_GRAYSCALE)或者0,强制转换成灰度图像,即1通道
            """
            img_data = cv2.imread("dataset/train" + os.sep + dirs +
                                  os.sep(fname))
            # label
            img_label = dirs
            tf_example = tf.train.Example(features=tf.train.features(
                feature={
                    'label':
                    tf.train.Feature(int64_list=tf.train.Int64List(
                        value=[img_label])),
                    'image_raw':
                    tf.train.Feature(bytes_list=tf.train.BytesList(
                        value=[img_data.tobytes()]))
                }))
            writer.write(tf_example.SerializeToString())
    print("Done training_set writing.")
    writer.close()
Exemple #3
0
    def pull_directory(self, parent_dir, output):

        for fd in self.read_directory(parent_dir):
            if PY3 and isinstance(fd, bytes):
                fd = fd.decode('utf-8')
            if fd in ('.', '..', ''):
                continue
            infos = self.get_file_info(posixpath.join(parent_dir, fd))
            if infos and infos.get('st_ifmt') == 'S_IFDIR':

                if parent_dir == "/":
                    new_folder = parent_dir + fd

                else:
                    new_folder = parent_dir + '/' + fd

                new_folder = re.sub('[<>:"|?*]', '_', new_folder)
                local_folder = os.path.join(output, new_folder.strip())
                if not os.path.exists(local_folder):
                    self.logger.info("Creating Folder: " + new_folder)
                    os.makedirs(local_folder)
                    # Change modify times
                    modify_time = datetime.fromtimestamp(
                        int(infos['st_mtime']) // 1000000000)
                    modTime = time.mktime(modify_time.timetuple())
                    os.utime(local_folder, (modTime, modTime))

                if new_folder is not '':
                    self.pull_directory(new_folder, output)

            else:
                if parent_dir == "/":
                    new_file = parent_dir + fd

                else:
                    new_file = parent_dir + '/' + fd

                new_file = re.sub('[<>:"|?*]', '_', new_file)
                local_file = output + os.sep() + new_file.strip()
                #self.download_file(new_file, local_file)
                parent_local_folder = (local_file[::-1].split("/"))
                local_single_file = parent_local_folder[0][::-1]
                del parent_local_folder[0]
                parent_local_folder = (os.sep.join(parent_local_folder))[::-1]
                if parent_local_folder.endswith(' '):
                    parent_local_folder = parent_local_folder[:-1]
                    local_file = os.path.join(parent_local_folder,
                                              local_single_file)
                if not os.path.exists(parent_local_folder):
                    os.makedirs(parent_local_folder)
                if infos is not None:
                    if infos['st_size'] == '0':
                        open(local_file, 'a').close()
                self.pull_file(new_file, local_file)
    def getStudioLogos(self):
            #fill list with all studio logos
            StudioImagesCustompath = xbmc.getInfoLabel("Skin.String(StudioImagesCustompath)")
            if StudioImagesCustompath:
                path = StudioImagesCustompath
                if not (path.endswith("/") or path.endswith("\\")):
                    path = path + os.sep()
            else:
                if xbmc.getCondVisibility("Skin.HasSetting(furniture.flags.colour)"):
                    path = "special://skin/extras/flags/colour/studios/"
                else:
                    path = "special://skin/extras/flags/studios/"

            if path != self.LastStudioImagesPath:
                self.LastStudioImagesPath = path
                allLogos = list()
                dirs, files = xbmcvfs.listdir(path)
                for file in files:
                    file = file.replace(".png","")
                    file = file.replace(".PNG","")
                    allLogos.append(file)

                self.studioLogosPath = path
                self.allStudioLogos = set(allLogos)
os.chdir("dirname")                     # 改变当前脚本工作目录;相当于shell下cd
os.curdir()                             # 返回当前目录: ('.')
os.pardir()                             # 获取当前目录的父目录字符串名:('..')
os.mkdir('dirname')                     # 生成单级目录;相当于shell中mkdir dirname
os.makedirs('dirname1/dirname2')        # 可生成多层递归目录
os.rmdir('dirname')                     # 删除单级空目录,若目录不为空则无法删除,报错;相当于shell中rmdir dirname
os.removedirs('dirname1')               # 若目录为空,则删除,并递归到上一级目录,如若也为空,则删除,依此类推
os.listdir('dirname')                   # 列出指定目录下的所有文件和子目录,包括隐藏文件,并以列表方式打印
os.remove('1.txt')                      # 删除一个文件
os.rename("oldname","newname")          # 重命名文件/目录/移动目录/,只能同设备(在同一块磁盘,不同设备看下面shutil模块)。
os._exit(n)                             # 直接推出,不抛异常,不执行相关清理工作。常用在子进程中
os.stat('path/filename')                # 获取文件/目录信息
    # l = os.stat('path/filename')
    # print(list(l))
    # print(list(l)[2])
os.sep()                                # 输出操作系统特定的路径分隔符,win下为"\\",Linux下为"/"
os.linesep()                            # 输出当前平台使用的行分隔符,win下为"\t\n",Linux下为"\n"
os.pathsep()                            # 输出用于分割文件路径的字符串 win下为;,Linux下为:
os.environ()                            # 获取系统环境变量
\os.system("bash command")              # 运行shell命令,直接显示,无法直接保存结果。os.system('ls')
os.system(cmd + "> /dev/null 2>&1")     # 将输出丢入黑洞
\os.popen()                             # 打开命令cmd或从命令cmd打开管道,返回值是连接到管道的文件对象
    # res = os.popen("free -m").read()
    #                 # 或readlines()    # 运行shell命令,获取执行结果赋值给变量。
    # print(res)
    res=os.popen("cat /etc/issue").read()
    res=res.split()[0]
    print(res)

os.path.abspath("/usr/bin")                   # 返回path的绝对路径
os.path.split("/usr/bin")                     # 将path分割成目录和文件名元组返回
Exemple #6
0
def test(base,tip,dimensions=None,\
        size=(200,200),sun=[10,-10,20.],direction=[-1,-1.,-1],\
        origin=None,focalPoint=None,\
        obj=None,name=None,type=None,file=None,info={},nAtTime=200):
  '''
  A simple test of the intersection algorithm
 
  A scan over an object is made and images produced
  in tests with the distances.

  '''

  from PyRatRay import PyRatRay
  from PyRatEllipsoid import PyRatEllipsoid
  from PyRatRay import PyRatRay
  from PyRatCylinder import PyRatCylinder
  from PyRatFacet import PyRatFacet
  from PyRatSpheroid import PyRatSpheroid
  from PyRatDisk import PyRatDisk
  from PyRatPlane import PyRatPlane
  from PyRatObjParser import PyRatObjParser
  from PyRatClone import PyRatClone
  import pylab as plt
  import matplotlib.cm as cm

  try:
    import pp
    isPP = True
  except:
    isPP = False

  import sys
  import os
  import pylab as plt

  type = type or str(globals()['__file__'].split(os.sep())[-1].split('.')[0])
  if 'verbose' in info:
    sys.stderr.write('Object: %s\n'%type)
  type = type.split('/')[-1]
  exec('from %s import %s'%(type,type))

  name = name or type[5:]
  obj = obj or eval('%s(base,tip,info=info)'%type)
  # ray direction
  direction = np.array(direction)
  direction /= sqrt(dot(direction,direction))
 
  if origin == None:
    origin = -direction * 6.0
  if focalPoint == None:
    focalPoint = origin*1.5
  # sun direction
  sun = np.array(sun)
  sun /= sqrt(dot(sun,sun))

  # image size
  #size = (200,200)

  # ray origins
  #origin = np.array([0,0,4]).astype(float)
  o = origin.copy()
  ray = PyRatRay(o,direction)

  # dimensions of the image in physical units
  if dimensions == None:
      dimensions = [2,2]

  result0 = np.zeros(size)
  result1 = np.zeros(size)
  result2 = np.zeros(size)
  sys.stderr.write('from %s in direction %s\n'%(str(origin),str(direction)))
  sys.stderr.write('Name: %s\n'%name)
  if len(sys.argv) > 1:
    try:
        ncpus = int(sys.argv[1])
    except:
        ncpus = -1
  else:
    ncpus = -1
 
  # tuple of all parallel python servers to connect with
  if ncpus != 0 and isPP:
    ppservers = ()
    if ncpus > 0:
      # Creates jobserver with ncpus workers
      job_server = pp.Server(ncpus, ppservers=ppservers)
    else:
      # Creates jobserver with automatically detected number of workers
      job_server = pp.Server(ppservers=ppservers)

    print "Starting pp with", job_server.get_ncpus(), "workers"

    sims = []
    l = float(size[0])
    index = []
    ray.sun = sun
    for ix in xrange(size[0]):
      o[0] = origin[0] + dimensions[0] * 2.*(ix-size[0]*0.5)/size[0]
      for iy in xrange(size[1]):
        o[1] = origin[1] + dimensions[1] * 2.*(iy-size[1]*0.5)/size[1]
        ray.length = PyRatBig
        d = (o - focalPoint)
        ray.direction = d/sqrt(dot(d,d))
        index.append((ix,iy))
        sims.append(ray.copy())

    sims = np.array(sims)
    index = np.array(index)
    results = []
    for i in xrange(0,len(sims),nAtTime):
      try:
        f = job_server.submit(obj.rayTreeMany,(sims[i:i+nAtTime],))  
        j = index[i:i+nAtTime]
      except:
        f = job_server.submit(obj.rayTreeMany,(sims[i:],))
        j = index[i:]
      results.append([j[:,0],j[:,1],f])

    if 'verbose' in info:
      sys.stderr.write('\nGathering results\n')
    l = size[0]*size[1]
    
    for c in xrange(len(results)):
      if 'verbose' in info and int(100.*(c+1)/l) % 5 == 0:
          sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%'%(100*(c+1)/l))
      r = results[c]
      f = r[2]
      thisResult = f()
      # this returns True,ray,n
      try:
       ww = np.where(np.array(thisResult)[:,0])[0]
       iix = r[0][ww]
       iiy = r[1][ww]
       for j,val in enumerate(np.array(thisResult)[ww]):
          if val[0]:
            ix = iix[j]
            iy = iiy[j]
            ray = val[1]
            n = val[2]/np.dot(val[2],val[2])
            lambert = dot(n,sun)
            if lambert < 0: lambert = 0
            result0[size[0]-1-ix,size[1]-1-iy] = lambert
            result1[size[0]-1-ix,size[1]-1-iy] = ray.tnear
            result2[size[0]-1-ix,size[1]-1-iy] = ray.tfar
      except:
        pass
  else:
    l = size[0]
    ray.sun = sun
    for ix in xrange(size[0]):
      o[0] = origin[0] + dimensions[0] * 2.*(ix-size[0]*0.5)/size[0]
      if 'verbose' in info and int(100.*(ix+1)/l) % 5 == 0:
          sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%'%(100*(ix+1)/l))
      for iy in xrange(size[1]):
        o[1] = origin[1] + dimensions[1] * 2.*(iy-size[1]*0.5)/size[1]
        ray.length = ray.tnear = ray.tfar =PyRatBig
        ray.origin = o
        d = (o - focalPoint) 
        ray.direction = d/sqrt(dot(d,d))
        try:
          hit,thisRay,n = obj.rayTree(ray.copy())
        except:
          hit = False
          try:
            print obj,ray
            print ray.copy()
          except:
            hit = False
        if hit:
          lambert = dot(n,sun)
          if lambert < 0: lambert = 0
          result0[size[0]-1-ix,size[1]-1-iy] = lambert
          result1[size[0]-1-ix,size[1]-1-iy] = thisRay.tnear
          result2[size[0]-1-ix,size[1]-1-iy] = thisRay.tfar
        else:
          missed = True
  if 'verbose' in info:
    sys.stderr.write('\nWriting results\n')
  plt.clf()
  plt.imshow(result0,interpolation='nearest',cmap=cm.Greys_r)
  if 'verbose' in info: sys.stderr.write('Mean: %f\n'%np.mean(result0))

  plt.colorbar()
  if not os.path.exists('tests'):
    os.makedirs('tests')
  plt.savefig('tests/PyRat%s.png'%name or file)
  plt.clf()
  plt.imshow(result1,interpolation='nearest',cmap=cm.Greys_r)
  plt.colorbar()
  plt.savefig('tests/PyRat%s-near.png'%name or file)
  plt.clf()
  plt.imshow(result2,interpolation='nearest',cmap=cm.Greys_r)
  plt.colorbar()
  plt.savefig('tests/PyRat%s-far.png'%name or file)
Exemple #7
0
#!/usr/bin/env python

import cv2, sys, os
from math import ceil
from glob import glob

interpolation = cv2.INTER_CUBIC
borderMode = cv2.BORDER_REPLICATE  # Notes: 边界处理的时候, 把其镜像复制
SEP = os.sep()


def crop_face(img, bbox, crop_sz, bbox_ext, extra_pad=0):
    shape = img.shape  # [height, width, channels]
    x, y, w, h = bbox

    jitt_pad = int(ceil(float(extra_pad) * min(w, h) / crop_sz))

    pad = 0
    if x < w * bbox_ext + jitt_pad:
        pad = max(pad, w * bbox_ext + jitt_pad - x)
    if x + w * (1 + bbox_ext) + jitt_pad > shape[1]:
        pad = max(pad, x + w * (1 + bbox_ext) + jitt_pad - shape[1])
    if y < h * bbox_ext + jitt_pad:
        pad = max(pad, h * bbox_ext + jitt_pad - y)
    if y + h * (1 + bbox_ext) + jitt_pad > shape[0]:
        pad = max(pad, y + h * (1 + bbox_ext) + jitt_pad - shape[0])
    pad = int(pad)

    if pad > 0:
        pad = pad + 3
        replicate = cv2.copyMakeBorder(img, pad, pad, pad, pad, borderMode)
Exemple #8
0
#模块
#import random
#num=random.randint(0,9)#
#print(num)

import os
print('path:', str(os.getcwd()))  #当前目录
print('sep:', str(os.sep()))
#print(os.curdir())    #.
#print(os.pardir())		#..

os.chdir('E:\\')  #cd
os.listdir("E:\\")  #列举
os.mkdir('E:\\a\\')
os.remove(path)  #删除文件
os.rmdir(path)  #删除目录
os.removedirs(path)  #递归删除
os.system('cmd')  #执行
os.curdir  #当前目录
os.pardir  #父级目录
os.listdir(os.curdir)  #显示当前文件夹
os.set  #路径分隔符
os.path.basename(path)  #返回文件名去除路径
os.path.dirname(path)  #返回路径
Exemple #9
0
# os 模块
# P85
# P89有个表
import os
os.system('ping www.baidu.com')
os.sep()  # 显示当前系统环境下路径分隔符
Exemple #10
0
def parse_and_send(file_name):
    '''
        Parse and send file
    '''
    if compress_large == True:
        file_size = os.stat(file_name).st_size
        if file_size > max_file_size:
            if os.name == "posix":
                ark_name = str(int(time.time())) + '.tgz'
                os.system('tar czfP ' + ark_name + ' ' +
                          os.path.relpath(file_name))
                os.rename(ark_name, cwd + 'archive' + os.sep() + ark_name)
            else:
                import shutil
                shutil.copy(file_name, cwd + str(int(time.time())) + '.xml')
            return

    try:
        xml = ET.parse(file_name).getroot()
    except:
        fan_print("xml file is corrupted")
        exit()

    reports = []
    i = 0
    for host in xml.findall('host'):
        report = {}
        addr = host.find('address').attrib['addr']

        hostname = ""
        if host.find('hostnames') is not None and host.find('hostnames').find(
                'hostname') is not None:
            hostname = host.find('hostnames').find('hostname').attrib['name']
        ports_array = []
        if host.find('ports') is not None:
            for port in host.find('ports').findall('port'):
                p = {}
                p['portid'] = port.attrib['portid']
                p['protocol'] = port.attrib['protocol']
                if len(port.findall('state')):
                    p['state'] = port.find('state').attrib['state']
                if len(port.findall('service')):
                    service = port.find('service')
                    p['service'] = {'name': service.attrib['name']}

                ports_array.append(p)

        if geoip_enabled == True:
            country_code = gi.country_code_by_addr(addr)
            if country_code == "": country_code = "A1"
        else:
            country_code = "A1"

        report['report'] = {
            'status': host.find('status').attrib['state'],
            'hostname': hostname,
            'address': addr,
            'geoip': {
                'country': country_code
            },
            'ports': ports_array
        }

        report['raw_xml'] = ET.tostring(host).replace('\n', '')
        report['tags'] = get_tags(report)

        if check_report(report, send_all):
            reports.append(report)

        if len(reports) > 19:
            i = i + len(reports)
            send_reports(reports, user, auth_hash, i)
            reports = []

    if len(reports):
        i = i + len(reports)
        send_reports(reports, user, auth_hash, i)
Exemple #11
0
def test(base,tip,dimensions=None,\
        size=(200,200),sun=[10,-10,20.],direction=[-1,-1.,-1],\
        origin=None,focalPoint=None,\
        obj=None,name=None,type=None,file=None,info={},nAtTime=200):
    '''
  A simple test of the intersection algorithm
 
  A scan over an object is made and images produced
  in tests with the distances.

  '''

    from PyRatRay import PyRatRay
    from PyRatEllipsoid import PyRatEllipsoid
    from PyRatRay import PyRatRay
    from PyRatCylinder import PyRatCylinder
    from PyRatFacet import PyRatFacet
    from PyRatSpheroid import PyRatSpheroid
    from PyRatDisk import PyRatDisk
    from PyRatPlane import PyRatPlane
    from PyRatObjParser import PyRatObjParser
    from PyRatClone import PyRatClone
    import pylab as plt
    import matplotlib.cm as cm

    try:
        import pp
        isPP = True
    except:
        isPP = False

    import sys
    import os
    import pylab as plt

    type = type or str(globals()['__file__'].split(os.sep())[-1].split('.')[0])
    if 'verbose' in info:
        sys.stderr.write('Object: %s\n' % type)
    type = type.split('/')[-1]
    exec('from %s import %s' % (type, type))

    name = name or type[5:]
    obj = obj or eval('%s(base,tip,info=info)' % type)
    # ray direction
    direction = np.array(direction)
    direction /= sqrt(dot(direction, direction))

    if origin == None:
        origin = -direction * 6.0
    if focalPoint == None:
        focalPoint = origin * 1.5
    # sun direction
    sun = np.array(sun)
    sun /= sqrt(dot(sun, sun))

    # image size
    #size = (200,200)

    # ray origins
    #origin = np.array([0,0,4]).astype(float)
    o = origin.copy()
    ray = PyRatRay(o, direction)

    # dimensions of the image in physical units
    if dimensions == None:
        dimensions = [2, 2]

    result0 = np.zeros(size)
    result1 = np.zeros(size)
    result2 = np.zeros(size)
    sys.stderr.write('from %s in direction %s\n' %
                     (str(origin), str(direction)))
    sys.stderr.write('Name: %s\n' % name)
    if len(sys.argv) > 1:
        try:
            ncpus = int(sys.argv[1])
        except:
            ncpus = -1
    else:
        ncpus = -1

    # tuple of all parallel python servers to connect with
    if ncpus != 0 and isPP:
        ppservers = ()
        if ncpus > 0:
            # Creates jobserver with ncpus workers
            job_server = pp.Server(ncpus, ppservers=ppservers)
        else:
            # Creates jobserver with automatically detected number of workers
            job_server = pp.Server(ppservers=ppservers)

        print "Starting pp with", job_server.get_ncpus(), "workers"

        sims = []
        l = float(size[0])
        index = []
        ray.sun = sun
        for ix in xrange(size[0]):
            o[0] = origin[0] + dimensions[0] * 2. * (ix -
                                                     size[0] * 0.5) / size[0]
            for iy in xrange(size[1]):
                o[1] = origin[1] + dimensions[1] * 2. * (
                    iy - size[1] * 0.5) / size[1]
                ray.length = PyRatBig
                d = (o - focalPoint)
                ray.direction = d / sqrt(dot(d, d))
                index.append((ix, iy))
                sims.append(ray.copy())

        sims = np.array(sims)
        index = np.array(index)
        results = []
        for i in xrange(0, len(sims), nAtTime):
            try:
                f = job_server.submit(obj.rayTreeMany, (sims[i:i + nAtTime], ))
                j = index[i:i + nAtTime]
            except:
                f = job_server.submit(obj.rayTreeMany, (sims[i:], ))
                j = index[i:]
            results.append([j[:, 0], j[:, 1], f])

        if 'verbose' in info:
            sys.stderr.write('\nGathering results\n')
        l = size[0] * size[1]

        for c in xrange(len(results)):
            if 'verbose' in info and int(100. * (c + 1) / l) % 5 == 0:
                sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%' % (100 *
                                                             (c + 1) / l))
            r = results[c]
            f = r[2]
            thisResult = f()
            # this returns True,ray,n
            try:
                ww = np.where(np.array(thisResult)[:, 0])[0]
                iix = r[0][ww]
                iiy = r[1][ww]
                for j, val in enumerate(np.array(thisResult)[ww]):
                    if val[0]:
                        ix = iix[j]
                        iy = iiy[j]
                        ray = val[1]
                        n = val[2] / np.dot(val[2], val[2])
                        lambert = dot(n, sun)
                        if lambert < 0: lambert = 0
                        result0[size[0] - 1 - ix, size[1] - 1 - iy] = lambert
                        result1[size[0] - 1 - ix, size[1] - 1 - iy] = ray.tnear
                        result2[size[0] - 1 - ix, size[1] - 1 - iy] = ray.tfar
            except:
                pass
    else:
        l = size[0]
        ray.sun = sun
        for ix in xrange(size[0]):
            o[0] = origin[0] + dimensions[0] * 2. * (ix -
                                                     size[0] * 0.5) / size[0]
            if 'verbose' in info and int(100. * (ix + 1) / l) % 5 == 0:
                sys.stderr.write('\b\b\b\b\b\b\b\b%.2f%%' % (100 *
                                                             (ix + 1) / l))
            for iy in xrange(size[1]):
                o[1] = origin[1] + dimensions[1] * 2. * (
                    iy - size[1] * 0.5) / size[1]
                ray.length = ray.tnear = ray.tfar = PyRatBig
                ray.origin = o
                d = (o - focalPoint)
                ray.direction = d / sqrt(dot(d, d))
                try:
                    hit, thisRay, n = obj.rayTree(ray.copy())
                except:
                    hit = False
                    try:
                        print obj, ray
                        print ray.copy()
                    except:
                        hit = False
                if hit:
                    lambert = dot(n, sun)
                    if lambert < 0: lambert = 0
                    result0[size[0] - 1 - ix, size[1] - 1 - iy] = lambert
                    result1[size[0] - 1 - ix, size[1] - 1 - iy] = thisRay.tnear
                    result2[size[0] - 1 - ix, size[1] - 1 - iy] = thisRay.tfar
                else:
                    missed = True
    if 'verbose' in info:
        sys.stderr.write('\nWriting results\n')
    plt.clf()
    plt.imshow(result0, interpolation='nearest', cmap=cm.Greys_r)
    if 'verbose' in info: sys.stderr.write('Mean: %f\n' % np.mean(result0))

    plt.colorbar()
    if not os.path.exists('tests'):
        os.makedirs('tests')
    plt.savefig('tests/PyRat%s.png' % name or file)
    plt.clf()
    plt.imshow(result1, interpolation='nearest', cmap=cm.Greys_r)
    plt.colorbar()
    plt.savefig('tests/PyRat%s-near.png' % name or file)
    plt.clf()
    plt.imshow(result2, interpolation='nearest', cmap=cm.Greys_r)
    plt.colorbar()
    plt.savefig('tests/PyRat%s-far.png' % name or file)
Exemple #12
0
    'mydata'
)  #creates binary file for storing data like dict., list and strings
shelfFile['cats'] = ['Zophie', 'Pooka', 'Simon', 'Fat-tail', 'Cleo']
print(shelfFile['cats'])
print(list(shelfFile.keys()))
print(list(shelfFile.values()))
shelfFile.close()
#-----------------------------------shutil------------------------------------------------------------------------------
print("Shutil")
# shutil.copy(location1,location2)
# shutil.copy(location1,location2\\newNameOfFile) for both copying and renaming
# shutil.copytree(location1,location2\\NewFolderName) for copying a whole folder and files
# shutil.move(location1,location2)
# shutil.move(location\\OldName,location\\NewName) for renaming
shutil.rmtree()  #delete folder and all of its content

#------------------------------------OS Module--------------------------------------------------------------------------
print('OS')
os.getcwd()
os.sep()
# os.unlink(filename) # for deleting a file
os.rmdir()  #for deleting a empty folder
'''for folderName,subfolderName, filenames in range os.walk('C:\\hello'):
    print("The folder is" + folderName)
    print("Subfoder "+ subfolderName )
    print("Files" +filenames)'''

#----------------------------------------Send2Trash---------------------------------------------------------------------
print('Send2Trash')
send2trash.send2trash(
    path='')  # will delete the file and send it to recycle bin
Exemple #13
0
import requests
import json
import os

##外层循环可添加

#接口地址
url = "https://api.vc.bilibili.com/dynamic_svr/v1/dynamic_svr/space_history?host_uid=7117493&offset_dynamic_id=0" 
#获取接口返回值
res_json = requests.get(url).json();
#获取要抓取的元素的值
cards = res_json['data']['cards']
#获取拉取下一轮动态的URL参数
offset_dynamic_id = cards[-1]['desc']['dynamic_id_str'] 

file_path = os.getcwd()+os.sep()+"imgs"


print(offset_dynamic_id)

#循环抓取本轮动态
for i in range(0,len(cards)):
	card_items = cards[i]['card']
	
	#指定键可能不存在
	try:
		img_card = json.loads(card_items)['item']['pictures']

		for j in range(0,len(img_card)):
			#文件地址
			img_src = img_card[j]['img_src']
 def test_random(self, urandom_function, sep_function):
     assert os.urandom(5) == 'fffff'
     assert os.sep() == '-'
Exemple #15
0
import os
"""
 Python 系统路径和其他一些操作模块
os  常用功能

os.sep()   符合当前系统的路径分隔符,linux/windows\
os.name()  返回操作系统类型Windows "nt" Linux "posix"
os.getcwd()返回当前的工作目录
os.listdir()列出指定目录下的目录文件
os.chdir()  修改当前的工作路径
os.mkdir()  创建目录
os.makedirs() 递归创建目录
os.remove()  删除文件
os.rmdir()   删除文件夹
os.removedirs() 递归删除文件夹
os.system()    执行系统命令
os.popen()     执行系统命令,会将结果以文件的形式返回
os.path.isfile()
os.path.isdir()
os.path.exists()
os.path.split()
"""
# 实现计算文件中的大写字母数
"""
1、把文件给读出来,然后遍历它在计数
"""
# os.chdir('D:\\') # 修改当前的工作路径
with open('D:\\a.txt') as  f :
    l = f.readline()
    print(l)
    count=0
#!/usr/bin/env python3
import os 
os.path.join('folder','folder2','folder3') #Va a devolver la ruta uniendo ocn el / o \ segun el OS

os.sep() #devuelve el separador en las rutas segun el OS

#REcorda que para hacer un / es necesario \/ para meter el caracter especial
os.getcwd()#TRae la direccion donde estoy parado
os.chdir('c://') # Cambio de carpeta

os.path.abspath('unArchivo.txt') #Devuelve la ruta relativa en absoluta
os.path.isabs('una ruta') #RETORNA SI EL PATH ES ABSOLUTO TRUE O FALSE

os.path.relpath(ruta1, ruta2) #Devuelve a relative path de la ruta2 en referencia  la ruta2

os.path.dirname() #REtorna la parte de la ruta que no incluye al archivo o al ultimo carpeta
os.path.basename() #REtrna la ultima carpeta / archivo

os.path.exists('unaRuta') #me dice si la ruta existe

os.path.isfile() #Dice si la ruta es para un archivo 
os.path.isdir() #DIce si la ruta es para una carpeta

os.path.getsize(unaARHCIVO) # devuelve el tamanio del archivo en bytes
os.listdir(unaDIreccionDeCarpeta) #devuelve lo que esta en ese directorio

# >>> for file in os.listdir(os.getcwd()):
# ...     if not os.path.isfile(os.path.join('/home/ivan/python-tuto',file)):
# ...             continue
# ...     size += os.path.getsize(os.path.join('/home/ivan/python-tuto',file))
# ... 
Exemple #17
0
def parse_and_send(file_name):
    '''
        Parse and send file
    '''
    if compress_large == True:
        file_size = os.stat(file_name).st_size
        if file_size > max_file_size:
            if os.name == "posix":  
                ark_name = str(int(time.time())) + '.tgz'
                os.system('tar czfP '+ ark_name + ' ' + os.path.relpath(file_name))
                os.rename(ark_name, cwd + 'archive' + os.sep() + ark_name)
            else:
                import shutil
                shutil.copy(file_name, cwd + str(int(time.time())) + '.xml')
            return

    try:
        xml = ET.parse(file_name).getroot()
    except:
        fan_print("xml file is corrupted")
        exit()

    reports = []
    i = 0
    for host in xml.findall('host'):
        report = {}
        addr = host.find('address').attrib['addr']

        hostname = "" 
        if host.find('hostnames') is not None and host.find('hostnames').find('hostname') is not None:
            hostname = host.find('hostnames').find('hostname').attrib['name']
        ports_array = []
        if host.find('ports') is not None:
            for port in host.find('ports').findall('port'):
                p = {}
                p['portid'] = port.attrib['portid']
                p['protocol'] = port.attrib['protocol']
                if len(port.findall('state')):
                    p['state'] = port.find('state').attrib['state']
                if len(port.findall('service')):
                    service = port.find('service')
                    p['service'] = { 'name' : service.attrib['name']}

                ports_array.append(p)

        if geoip_enabled == True:
            country_code = gi.country_code_by_addr(addr)
            if country_code == "": country_code = "A1"
        else:
            country_code = "A1"

        report['report'] = {
            'status' : host.find('status').attrib['state'],
            'hostname' : hostname,
            'address' : addr,
            'geoip' : { 'country': country_code },
            'ports' : ports_array
        }

        report['raw_xml'] = ET.tostring(host).replace('\n', '')
        report['tags'] = get_tags(report)

        if check_report(report, send_all):
            reports.append(report)

        if len(reports) > 19:
            i = i + len(reports) 
            send_reports(reports, user, auth_hash, i)
            reports = []


    if len(reports):
        i = i + len(reports)
        send_reports(reports, user, auth_hash, i)
Exemple #18
0
#coding=utf-8
import os
os.getcwd()  #获取目录
os.chdir(r'C:\user')  #切换目录
os.curdir()  #获取当前目录
os.pardir()  #获取当前目录的父目录
os.makedirs(r'C:\a\b\v\d')  #创建目录
os.removedirs(r'C:\a\b\v\d')  #目录为空则删除
os.mkdir()  #单次创建
os.rmdir()  #d单次删除
os.listdir()  #列出当前目录下的文件及文件夹
os.remove()  #删除文件
os.rename()  #重命名文件
os.stat()  #返回文件信息
os.sep()  #输出当前操作系统的路径分隔符
os.linesep  #输出当前平台的行终止符,win下为\t\n linux下为\n
os.pathsep  #输出用于分割文件路径的字符串。环境变量中的分割符
os.environ  #当前环境变量
os.system()  #执行系统命令
os.path.abspath()  #返回绝对路径
os.path.split()  #分割目录和文件名
os.path.dirname()  #返回path的路径名
os.path.basename()  #返回path的文件名
os.path.exists()  #判断路径是否存在
os.path.isabs()  #是否是绝对路径
os.path.isfile()  #是否文件
os.path.isdir()  #是否为目录
os.path.join()  #将多个路径组合返回
os.path.getatime()  #返回最后存取时间
os.path.getmtime()  #返回最后修改时间