wiggin's Blog


  • 首页

  • 标签

  • 分类

Children Resource

Posted on 2025-02-25 | In 清单

Python中文件和目录操作

Posted on 2021-04-28 | In Python

命令汇总

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import os
import shutil
os.getcwd()  # 获取当前工作目录,非脚本目录
os.listdir()  # 返回指定目录下的所有文件和目录,非递归
os.remove()  # 删除文件
os.removedirs()  #删除目录
os.path.isfile()  # 检验给出的路径是否是一个文件
os.path.isdir()  # 检验给出的路径是否是一个目录
os.path.isabs()  # 判断是否是绝对路径
os.path.exists()  # 检验给出的路径是否真实存在
os.path.split()  # 返回一个路径的目录名和文件名
os.path.splitext()  # 分离文件扩展名
os.path.dirname()  # 获取文件路径名
os.path.basename() # 获取一个绝对路径下的文件名
os.system()  # 运行shell命令
os.rename(old,new) # 重命名文件或目录
os.makedirs(r"c:\python\test")  # 创建多级目录
os.mkdir("test")  # 创建单个目录
os.exit()  # 终止当前进程
os.path.getsize(filename)  # 获取文件大小
os.mknod("test.txt")  # 创建空文件
shutil.copyfile("oldfile","newfile")  # oldfile和newfile都只能是文件
shutil.copytree("olddir","newdir")  # olddir和newdir都只能是目录,且newdir必须不存在
shutil.move("oldpos","newpos")  # 移动文件或目录
shutil.rmtree("dir")  # 删除目录,与os.removedirs()相同
os.path.join(“home”, "me", "mywork")  # 路径连接

常用命令

1. 获取目录中的文件名os.listdir(dir_name)

1
2
3
4
5
import os
#获取当前目录中的文件名
os.listdir('.')
#获取当前目录中的.vtt文件
vttFileNameList = [f for f in os.listdir('.') if '.vtt' in f]

2. 获取当前路径os.getcwd()

1
2
3
4
5
6
# 当前路径
sys.path[0]
sys.argv[0]
os.getcwd()
os.path.abspath(__file__)
os.path.realpath(__file__)

3. 创建目录os.mkdir(dir_name)或os.makedirs(dir_name)

1
2
3
4
5
6
7
创建单个目录
import os
os.mkdir('public')

创建多级目录
import os
os.makedirs('public/attachment/201706/21/16')

4. 返回文件或目录是否存在

1
2
3
4
os.path.exists(fileName)   
import os
if not os.path.exists('foldername'):
  os.mkdir('foldername')

5. 删除目录或文件

1
2
3
4
5
6
7
8
9
os.remove(p)
os.rmtree(p)  
def del_dir_or_file(p):
if os.path.isdir(p):
    os.rmtree(p)
    print "delete dictionary %s" % p
else if os.path.isfile(p):
    os.remove(p)
    print "delete file %s" % p

6. 添加path环境变量

1
2
# 用于加载非本目录的模块
sys.path.append('..')

7. 目录拼接

1
os.path.join(dir_name, file_name)

8. 移动文件

1
2
3
4
5
6
shutil.move
shutil.copyfile("oldfile","newfile")  # oldfile和newfile都只能是文件
shutil.copytree("olddir","newdir")  # olddir和newdir都只能是目录,且newdir必须不存在
shutil.move("oldpos","newpos")  # 移动文件或目录
shutil.rmtree("dir")  # 删除目录,与os.removedirs()相同
os.path.join("home”, "me", "mywork")  # 路径连接

9. 判断是文件还是目录os.path.isdir()和os.path.isfile()

1
2
3
import os
os.path.isdir(filename)
os.path.isfile(filename)

10. 执行shell命令

  • os.system(cmd)的返回值只会有0(成功),1,2
  • os.popen(cmd)会吧执行的cmd的输出作为值返回
  • subprocess.call()
1
2
3
4
5
6
7
8
9
# 代码示例:
比如,我想得到ntpd的进程id,就要这么做:
os.popen('ps -C ntpd | grep -v CMD |awk '{ print $1 }').readlines()[0]
获取执行dir命令的返回内容
os.popen('dir').readlines()

from subprocess import call
command = "youtube-dl https://www.youtube.com/watch?v=NG3WygJmiVs -c"
call(command.split(), shell=False)

复杂应用

1. 读取目录下所有TXT文件,并写入TXT文档中

1
2
3
4
5
6
7
8
9
10
11
12
13
import os
def write_txt(dist, big_txt):
    with open("{}{}".format(big_txt,".txt"), "a") as fw:
        lines = []
        for file in os.listdir(dist):
            file_path = r"{}\{}".format(dist,file)
            if os.path.isfile(file_path) and file.endswith(".py"):
                with open(file_path, "r") as fr:
                    lines = lines + fr.readlines()
        for line in lines:
            fw.write(line)
        
write_txt('G:\Work\workspace_python\selenium','111')

2. 日志记录

1
2
3
4
5
6
7
import datetime, time
def write_log(log_str):
    curr_day = datetime.date.today().strftime('%Y-%m-%d')
    curr_time = time.strftime('%H:%M:%S')
    with open('log.txt', 'a') as f:
        f.write("{}{}{} \n".format(curr_day, curr_time, log_str))
write_log('testsetst')

3. 使用Python批量重命名文件

使用的函数

  • os.listdir(‘.’) #列出目录中所有文件名
  • os.path.join(path, file_name) #拼接完整文件名
  • os.path.isdir(file_name) #判断是否是目录
  • os.path.splittext(file_name) #分离文件名[0]和扩展名[1]
  • os.rename(old_name, new_name) #重命名文件
1
2
3
4
5
6
import os
path = r"I:\desktop\Python"
for file in os.listdir(path):
    new_name = file.replace('51CTO下载-', '')
    os.rename(os.path.join(path, file), os.path.join(path, new_name))
    print(os.path.join(path, new_name))

4. 判断文件内容是二进制还是文本

1
2
3
4
def is_binary_string(bytes):
    textchars = bytearray({7, 8, 9, 10, 12, 13, 27} | set(range(0x20, 0x100)) - {0x7f})
    return bool(bytes.translate(None, textchars))
is_binary_string(open(filename, 'rb').read(1024))

泛微OA7.0常见问题解决

Posted on 2021-04-22 | In Work

资源

  • OA手机端下载(泛微OA7.0版本对应移动客户端版本为4.8)

OA插件下载与安装

  1. 下载OA插件
  2. 解压并进入EcologyPlugin目录

  1. 鼠标双击执行目录中的【受信站点设置.reg】,设置IE配置
  2. 选择目录中的【setup.bat】,点击鼠标右键选择【以管理员身份运行】,根据提示执行安装操作
  3. 拷贝目录中的【OA系统入口】到桌面,鼠标双击即可打开IE浏览器进入OA系统
  4. 登录OA系统后,按下图提示进入控件检测界面,逐一完成三项检测,出现的IE浏览器弹窗都选择【是】


问题一:使用360浏览器无法OA

原因:近期360浏览器升级后,默认没有使用IE内核,就不能打开OA表单

解决:下载OA系统入口快捷方式,解压后放到桌面,使用时直接双击该快捷方式打开

问题二:附件无法上传

情况一:看不到上传按钮

原因:OA中上传使用了flash插件,flash发布新版本后可能导致原有控件失效

解决:下载Flash中心,安装后,点击【一键检测】,发现问题点击【一键修复】,之后重启IE浏览器再次尝试

情况二:无法弹窗选择选择上传文件

原因:弹窗被浏览器阻止

解决:在IE浏览器中,点击右上角齿轮,进入【Internet选项】,进入【隐私】选项页,取消【启用弹出窗口阻止程序】,重启IE浏览器再次尝试


解决pip下载太慢

Posted on 2020-04-05 | In Python

一、临时使用

命令行中使用pip install -i指定源安装

1
pip install Django==1.11.6 -i https://pypi.douban.com/simple

二、永久修改

  • Linux:修改~/.pip/pip.conf;
  • Windows:直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,新建文件pip.ini

配置文件内容如下

1
2
3
4
5
6
7
8
[global]
index-url = https://pypi.douban.com/simple
#没有这句会包warning
trusted-host = pypi.douban.com
#版本不检查
disable-pip-version-check = true
#超时时间设置
timeout = 120

三、几个中国的PyPI镜像源

1
2
3
4
5
6
7
阿里云 速度最快 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
Python官方 https://pypi.python.org/simple/
v2ex http://pypi.v2ex.com/simple/
中国科学院 http://pypi.mirrors.opencas.cn/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/

信息更新

Posted on 2020-03-13 | In 清单

20200323更新地址:
45.32.6.137:50011

12…7

wiggin

31 posts
9 categories
7 tags
RSS
© 2025 wiggin
Powered by Hexo
|
Theme — NexT.Gemini v5.1.4
本站访客数: