- 论坛徽章:
- 0
|
- #!/usr/bin/python
- #Filename: backup_cproject.py
- import os
- import sys
- if len(sys.argv) < 3:
- print 'usage ./backup_cproject.py soure_dir target_dir'
- sys.exit()
-
- if sys.argv[1].startwith('-'):
- option = sys.argv[1][1:]
- if option == 'help':
- print 'usage ./backup_cproject.py soure_dir target_dir'
- else:
- print 'Error option, you can only use option -help'
- sys.exit()
- source = sys.argv[1]
- print source
- target = sys.argv[2]
- print target
- if os.path.isdir(source):
- print '%s is not directory' %(source)
- sys.exit()
- if os.path.isdir(target):
- print '%s is not directory' %(target)
- sys.exit()
- filename = source[source.rfind('/'):] + 'tar.gz'
- os.path.chdir(source)
- #the tar.gz file place in compress path
- compress_path = target + filename
- tar_command = "tar -zcvf '%s' %s" %(compress_path)
- if os.system(tar_command) == 0:
- print 'Successfu backup to', target
- else:
- print 'Backup failed'
复制代码 初学Python, 用了个 os.path.isdir() 检测路径是否为目录,
但是不管我输什么路径,一直返回 false.
我把 source 设置成 /home, 还是返回 /home is not directory,不知道为什么,希望知道的人帮一下忙,感激不尽! |
|