python目录判断与创建

1、完成目录判断
2、完成文件创建
3、完成cpickle模块化写与读

import cPickle as p,os

dirlis = os.getcwd()
    dirlis_new = dirlis + "\\"+"pytTest"
    print dirlis_new
    if not os.path.exists(dirlis_new):
        os.mkdir(dirlis_new)
        print "目录创建成功!!!"
    else:
        print "目录已经存在!!!"
    os.chdir(dirlis_new)
    print "进入目录,开始新建文件"
    file_name = "python.py"
    with open(file_name,"w") as fo:
        fo.write("#coding=utf-8")
    #p.dump(open(file_name),True)
    f1 = file("tem.pkl","a")
    p.dump("\nprint \"wgood\" ",f1,True)
    f1.close()
    fr = file("tem.pkl","r")
    ar = p.load(fr)[1]
    print ar

原文地址:http://blog.51cto.com/357712148/2060059

时间: 2024-08-25 06:03:19

python目录判断与创建的相关文章

练习,判断并创建不存在的文件或目录

日常python脚本练习,判断并创建不存在的文件或目录 #!/usr/bin/env python #-*- coding:utf-8 -*- import os import time a = 'mkdir aab'    b=os.listdir('/root/www')  #使用os.listdir查看/root/www目录下的文件 print b if  'aab'  in b:   #如果/root/www目录下没有aab目录或文件 print '文件或目录已经存在' else: os

使用for循环创建在指定目录下批量创建文件并重命名所有文件

要求1: 使用for循环在/root/scripts/practice/q1/oldboy 目录下创建十个文件.名称依次为: oldboy-1, oldboy-2, ..... oldboy-10. 脚本实现: #!/bin/bash #Question1 shopt -s expand_aliases alias ll="ls -l" cd /root/scripts/practice/q1 #首先判断目录是否存在,不存在则创建目录 if [ ! -e oldboy ] then  

Python pip换源 创建虚拟环境 luffy项目配置(数据库bug)

目录 pip安装源 基本使用 永久配置安装源 Windows Linux 文件配置内容 虚拟环境安装 Windows Linux 使用 luffy目录重构 日志文件配置 封装项目异常处理 二次封装Response模块 路由组件配置 数据库配置 Django 2.x 一些版本pymysql兼容问题 pip安装源 基本使用 采用国内的源,加速下载模块速度 常用的pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里:https://mirrors.aliyu

SAP文件夹的判断与创建

[转自 http://blog.csdn.net/saphome/article/details/6956918] SAP文件夹存在的判断与创建 2010-08-29 20:15 相关函数:WS_QUERY:判断文件夹路径是否存在.GUI_CREATE_DIRECTORY:创建文件夹. 检查指定的文件夹是否存在,若不存在则创建新文件夹. REPORT Z_EXAMPLE_01                            . parameter: l_file type localfil

python isinstance 判断各种类型的小细节

1. 基本语法 isinstance(object, classinfo) Return true if the object argument is an instance of the classinfo argument, or of a (direct, indirect or virtual) subclass thereof. Also return true if classinfo is a type object (new-style class) and object is

android判断和创建快捷方式(4.03测试通过)

整理了网上的创建方式的代码,对于快捷方式的判断使用系统api获取当前启动器来处理,这样系统定制过或者启动器不一样也没关系 . 一加权限和声明目标activity <!-- 创建快捷方式 --> <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> <uses-permission android:name="com.android.l

2.python if 判断 if elif 判断

python if判断 if elif 判断 a = 1; b = 1;b if  a>b: print(" a > b ") else: print(" a不大于b吧 ") if a>b: print(" a > b ") elif a<b: print(" a < b ") else: print(" a = b ") 注 : python 中 if 判断执行语句一定

python中判断语句用两个or连接的奇葩

学python的时候犯的一个错误,放在这吧.就是在循环某个列表的时候不要去操作它,这是容易忽略的一个地方.所以如果要操作某个列表本身,那么先把该列表copy一份,然后再读取的时候读copy的那份.操作原来的列表. 正确的如下: import re a="hen/zg /zg qd/a /a ,/x /x hen/zg /zg xh/v /v " b=re.split('[ ]', a) b_copy=b[:] print b cixing=["/x","/

java python oracle判断字符串是否为数字的函数

java public boolean isNumeric(String str){ Pattern pattern = Pattern.compile("^-?[0-9]+\\.?[0-9]+"); Matcher isNum = pattern.matcher(str); if( !isNum.matches() ){ return false; } return true; } python def isNum(value): try: int(value) + 1 except