+ -
当前位置:首页 → 问答吧 → 莪最近在查看别人的源码、发现他定义的字符串前面都有个r、是什么意思

莪最近在查看别人的源码、发现他定义的字符串前面都有个r、是什么意思

时间:2011-07-19

来源:互联网

本帖最后由 klobodnf 于 2011-07-19 11:52 编辑

莪最近在查看别人的源码、发现他定义的字符串前面都有个r、是什么意思
zip = r"E:\program\7-Zip\7z.exe"
flFileList = open(r"G:\impl\build\path.txt", 'r')








为了帮助大家理解、莪把代码完整的贴出来吧
  1. #!/usr/bin/python
  2. # encoding:utf-8
  3. # Filename: copyFileFromList.py

  4. import os
  5. import sys
  6. import time
  7. import shutil

  8. zip = r"E:\program\7-Zip\7z.exe"
  9. flFileList = open(r"G:\impl\build\path.txt", 'r')
  10. flFileSourceHistory = open(r"G:\impl\build\SourceDirHistory.txt", 'w')
  11. dirZipDest = r"I:\pre-realease\201107"

  12. dtDestRoot = {}
  13. dtDestRoot['zoshow'] = r'G:\impl\zip\zoshow'
  14. dtDestRoot['xiutuan'] = r'G:\impl\zip\xiutuan'
  15. dtDestRoot['tuanhuo'] = r'G:\impl\zip\tuanhuo'
  16. dtDestRoot['365wine'] = r'G:\impl\zip\365wine'
  17. dtDestRoot['search'] = r'G:\impl\zip\search'
  18. dtDestRoot['newcms'] = r'G:\impl\zip\newcms'
  19. dtDestRoot['zuc'] = r'G:\impl\zip\zuc'
  20. dtDestRoot['thexiucms'] = r'G:\impl\zip\thexiucms'
  21. sSelectProject = """
  22. Please select a project:
  23.         zoshow
  24.         xiutuan
  25.         tuanhuo
  26.         365wine
  27.         search
  28.         newcms
  29.         zuc
  30.     thexiucms
  31. Enter choice: """

  32. sourceRoot = raw_input('enter source root: ')
  33. if not os.path.exists(sourceRoot):
  34.         print '文件夹不存在: ' + sourceRoot
  35.         sys.exit()

  36. # 检查是否存在目标文件夹(destRoot),如果存在则重命名备份,不存在则新建文件夹。
  37. destRootChoice = raw_input(sSelectProject)
  38. if destRootChoice in dtDestRoot.keys():
  39.         destRoot = dtDestRoot[destRootChoice]
  40.         if os.path.exists(destRoot):
  41.                 os.rename(destRoot, destRoot + '_bak' + time.strftime('%Y%m%d') + '-' + time.strftime('%H%M'))
  42.                 os.mkdir(destRoot)
  43.         else:
  44.                 os.mkdir(destRoot)
  45. else:
  46.         print 'no such project'
  47.         sys.exit()
  48. #destRoot = raw_input('enter dest root: ')
  49. #if os.path.exists(destRoot):
  50. #        os.rename(destRoot, destRoot + '_bak' + time.strftime('%Y%m%d') + '-' + time.strftime('%H%M'))
  51. #        os.mkdir(destRoot)
  52. #else:
  53. #        os.mkdir(destRoot)

  54. ltFile = flFileList.readlines()
  55. for singleFile in ltFile:
  56.         singleFile = singleFile.strip()
  57.         if (os.path.exists(singleFile) and os.path.isfile(singleFile)):
  58.                 sourceFolder = os.path.split(singleFile)[0]
  59.                 destFolder = destRoot + sourceFolder.replace(sourceRoot, '')
  60.                 if not os.path.exists(destFolder):
  61.                         os.makedirs(destFolder)
  62.                 shutil.copy2(singleFile, destFolder)
  63.                 #print singleFile, destFolder
  64. flFileSourceHistory.close()
  65. flFileList.close()

  66. dir2Zip = destRoot
  67. flZip = os.path.split(destRoot)[0] + os.sep + os.path.split(destRoot)[1] + '-' + time.strftime('%Y%m%d') + '-' + time.strftime('%H%M') + '.zip'
  68. zip_command = "%s a -tzip %s %s" % (zip, flZip, dir2Zip)
  69. os.system(zip_command)

  70. # 移动到I:\pre-realease\
  71. os.rename(flZip, dirZipDest + os.sep + os.path.split(flZip)[1])
  72. print "move %s to \n%s" % (flZip, dirZipDest + os.sep + os.path.split(flZip)[1])
复制代码

作者: klobodnf   发布时间: 2011-07-19

明白了、是强制禁止转义的意思

作者: klobodnf   发布时间: 2011-07-19

回复 klobodnf
多余,用不着r。open(‘G:/impl/build/path.txt’, 'r')就行。

作者: 106033177   发布时间: 2011-07-19