用于同步两个文件夹的库
sync-folders的Python项目详细描述
同步文件夹包
大家好!这是Python“sync folders”上我的包的存储库。在
目录
- Table of contents
- Motivation
- Build status
- Code style
- Dependencies
- Features
- Installation
- Importing
- Fast usage 在
- API
- main.sync( path_a, path_b )
- main.files( path )
- main.dirs( path )
- main.read_file( path )
- main.write_file( path, data )
- main.list_dir( path )
- main.get_files( path )
- main.read_zip( path )
- main.files_in_zip( path )
- main.extract( path_to_zip, path_to_file )
- main.create_zip( files, path)
- cleaner.cleaner( folders, limit )
- purgelog.purgelog( log-file, limit, number )
- Code Example
- Tests
- Contributing
- Credits
- License
动机
有一天我做了一个脚本,它根据文件夹的文件日期同步两个文件夹。以后我想在PyPi上创建包。另外,除了syncing之外,我还为处理文件添加了一些新功能。在
生成状态
在这里您可以看到continuous integration/continuous deployment的生成状态:
代码样式
我使用Codacy来自动化我的代码质量。在
依赖关系
You can see all dependencies from
requirements.txt
here.
特点
使用我的软件包,您可以sync两个文件夹,manage日志文件,delete空文件夹和旧文件,读取并创建zip存档文件。在
安装
首先安装Python。在
If you don't have pip, install it.
然后输入终端:
pip install sync-folders --upgrade
导入
^{pr2}$快速使用
此模块的用法示例
同步
main.sync('./test_a','./test_b')# Expected creation of `logs.txt`
文件输出
main.files('./')""" Expected output.gitattributes Last Modified: 18 Jun 2020, 08 52.gitignore Last Modified: 10 Jun 2020, 06 39CONTRIBUTING.md Last Modified: 23 Jun 2020, 12 26LICENSE Last Modified: 09 Jun 2020, 18 10README.md Last Modified: 23 Jun 2020, 18 20requirements.txt Last Modified: 10 Jun 2020, 15 00setup.cfg Last Modified: 10 Jun 2020, 07 08setup.py Last Modified: 21 Jun 2020, 14 03_config.yml Last Modified: 10 Jun 2020, 09 28"""
Dirs输出
main.dirs('./tests')""" Expected output.venvtest_atest_b"""
读取文件数据
print(main.read_file('./index.py'))""" Expected outputfrom sync_folders import main, purgelog, cleanerprint(main.read_file('./index.py'))"""
写入文件
Not an appending
main.write_file('./test.txt','your text')
目录
main.list_dir('./')""" Expected result['.git', '.github', '.venv', 'sync_folders', 'tests', 'util']"""
文件列表
main.get_files('./')""" Expected result[ {'name': 'python-package.yml', 'date': 1592564708.6109703, 'date_str': '19 Jun 2020, 11 05'}, {'name': 'python-publish.yml', 'date': 1591772746.2324488, 'date_str': '10 Jun 2020, 07 05'}]"""
读取zip存档
main.read_zip('./test.zip')""" Expected outputtest_a/ Compress size: 0.0 in KB Filesize: 0.0 in KBtest_a/test.docx Compress size: 8.490234375 in KB Filesize: 11.1572265625 in KBtest_b/ Compress size: 0.0 in KB Filesize: 0.0 in KB"""
存档
中的元素列表main.files_in_zip('test.zip')""" Expected result['test_a/', 'test_a/test.docx', 'test_b/']"""
从存档中提取元素
main.extract('test.zip','test_a/test.docx')# Expected creation of `test_a/test.docx`
创建存档
main.create_zip(['_config.yml','LICENSE','setup.py'],'./test.zip')# Expected creation of `test.zip`
清洁剂
cleaner.cleaner(['./test_a','./test_a/test_c','./test_b'],5)# Expected creation of `logs.txt`""" Expected outputSTART TIME: Tue Jun 23 22:01:00 2020Total deleted size: 0.0 MBTotal deleted files: 0Total deleted empty folders: 3FINISH TIME: Tue Jun 23 22:01:00 2020"""
Purgelog
purgelog.purgelog('./logs.txt',5,2)""" Expected outputCopied: logs.txt to 1_logs.txt"""# Expected creation of `1_logs.txt`
美国石油学会
在主同步(路径a,路径b)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
path_a | ^{ | ^{ | ^{ | the path to the directory |
path_b | ^{ | ^{ | ^{ | the path to the directory |
在main.files文件(路径)
^{tb2}$在主目录(路径)
^{tb2}$在main.read_文件(路径)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
path | ^{ | ^{ | ^{ | the path to the file |
在main.write_文件(路径,数据)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
path | ^{ | ^{ | ^{ | the path to the file |
data | ^{ | ^{ | ^{ | the content |
在主目录(路径)
^{tb2}$在main.get_文件(路径)
^{tb2}$在main.read\u邮编(路径)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
path | ^{ | ^{ | ^{ | the path to the archive |
在main.files_-zip格式(路径)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
path | ^{ | ^{ | ^{ | the path to the archive |
在主要.摘录(path_to_zip,path_to_file)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
path_to_zip | ^{ | ^{ | ^{ | the path to the archive |
path_to_file | ^{ | ^{ | ^{ | the path to the need file for extracting |
在main.create_-zip文件(文件,路径)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
files | ^{ | ^{ | ^{ | the list of pathes to the files for archiving |
path | ^{ | ^{ | ^{ | the path to the new archive |
在清洁工。清洁工(文件夹,限制)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
folders | ^{ | ^{ | ^{ | the array of the folders |
limit | ^{ | ^{ | ^{ | the limit of the days for comparing |
在purgelog.purgelog公司(日志文件,限制,编号)
Name | Type | Argument | Default | Description |
---|---|---|---|---|
log-file | ^{ | ^{ | ^{ | the path to the log-file |
limit | ^{ | ^{ | ^{ | the limit of the maximum memory |
number | ^{ | ^{ | ^{ | the number of the maximum available number of the logs file |
代码示例
这里你可以看到同步两个文件夹的函数
# Main function for synchronize two foldersdefsync(path_a=None,path_b=None):ifnotpath_aornotpath_b:raiseNameError('Required path to both dirs')logs=''files_in_a=get_files(path_a)files_in_b=get_files(path_b)same_files=[]forfile_ainfiles_in_a:forfile_binfiles_in_b:iffile_b['name']==file_a['name']:# compare datesiffile_b['date']<file_a['date']:# changeshutil.copy2(path_a+'/'+file_a['name'],path_b)logs+=f"Change {file_a['name']} in {path_b}"+'\n'same_files.append(file_b['name'])forfile_ainfiles_in_a:ifnotfile_a['name']insame_files:# move to bshutil.copy2(path_a+'/'+file_a['name'],path_b)logs+=f"Create {file_a['name']} in {path_b}"+'\n'write_file('./logs.txt',logs)
测试
使用pytest进行单元测试:
公司名称:微笑:我给你从link到{a49},在那里你可以看到我所有的测试。在
贡献
欢迎拉取请求。对于重大变化,请先打开一个问题,讨论您希望更改的内容。再看看CONTRIBUTING.md。在
学分
链接到帮助我构建此项目的视频和文章:
- https://realpython.com/working-with-files-in-python/
- https://www.youtube.com/watch?v=sb3118xptEM&list=PLg5SS_4L6LYt7Wmh8zBKjZ_ltaoDXSEmk&index=4
- https://www.youtube.com/watch?v=mjyKFuwXVxY&list=PLg5SS_4L6LYt7Wmh8zBKjZ_ltaoDXSEmk&index=5
许可证
麻省理工学院mezgoodle
- 项目
标签: