免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2123 | 回复: 1
打印 上一主题 下一主题

os.path.isdir() 函数一直返回false [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-18 09:54 |只看该作者 |倒序浏览

  1. #!/usr/bin/python
  2. #Filename: backup_cproject.py
  3. import os
  4. import sys


  5. if len(sys.argv) < 3:
  6.         print 'usage ./backup_cproject.py soure_dir target_dir'
  7.         sys.exit()
  8.        
  9.         if sys.argv[1].startwith('-'):
  10.                 option = sys.argv[1][1:]
  11.                 if option == 'help':
  12.                         print 'usage ./backup_cproject.py soure_dir target_dir'
  13.                 else:
  14.                         print 'Error option, you can only use option -help'
  15.                         sys.exit()

  16. source = sys.argv[1]
  17. print source
  18. target = sys.argv[2]
  19. print target

  20. if os.path.isdir(source):
  21.         print '%s is not directory' %(source)
  22.         sys.exit()
  23. if os.path.isdir(target):
  24.         print '%s is not directory' %(target)
  25.         sys.exit()

  26. filename = source[source.rfind('/'):] + 'tar.gz'

  27. os.path.chdir(source)

  28. #the tar.gz file place in compress path
  29. compress_path = target + filename
  30. tar_command = "tar -zcvf  '%s' %s" %(compress_path)

  31. if os.system(tar_command) == 0:
  32.         print 'Successfu backup to', target
  33. else:
  34.         print 'Backup failed'
复制代码
初学Python, 用了个 os.path.isdir() 检测路径是否为目录,
但是不管我输什么路径,一直返回 false.
我把 source 设置成  /home, 还是返回  /home is not directory,不知道为什么,希望知道的人帮一下忙,感激不尽!

论坛徽章:
0
2 [报告]
发表于 2013-05-18 10:41 |只看该作者
执行脚本的用户,,对目录没有权限?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP