免费注册 查看新帖 |

Chinaunix

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

关于DOS2UNIX的问题(批量转换) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-09-08 11:49 |只看该作者 |倒序浏览
关于DOS2UNIX的问题(批量转换)

有好几百个文件;一个总的目录下面有很多子目录;有的文件有;有的文件没有;我该怎么办啊?

    

论坛徽章:
0
2 [报告]
发表于 2004-09-08 12:41 |只看该作者

关于DOS2UNIX的问题(批量转换)

原帖由 "kent.deng" 发表:
关于DOS2UNIX的问题(批量转换)

有好几百个文件;一个总的目录下面有很多子目录;有的文件有;有的文件没有;我该怎么办啊?

:twisted:  :twisted:  :twisted:


  1. #! /usr/bin/env python
  2. #########################################################
  3. # Use: "python fixeoln_dir.py [tounix|todos] patterns?".
  4. # convert end-lines in all the text files in the current
  5. # directory (only: does not recurse to subdirectories).
  6. # Resuses converter in the single-file _one version.
  7. #########################################################

  8. import sys, glob
  9. from fixeoln_one import convertEndlines
  10. listonly = 0
  11. patts = ['*.py', '*.pyw', '*.txt', '*.cgi', '*.html',    # text file names
  12.          '*.c',  '*.cxx', '*.h',   '*.i',   '*.out',     # in this package
  13.          'README*', 'makefile*', 'output*', '*.note']

  14. if __name__ == '__main__':
  15.     errmsg = 'Required first argument missing: "todos" or "tounix"'
  16.     assert (len(sys.argv) >;= 2 and sys.argv[1] in ['todos', 'tounix']), errmsg

  17.     if len(sys.argv) >; 2:                 # glob anyhow: '*' not applied on dos
  18.         patts = sys.argv[2:]              # though not really needed on linux
  19.     filelists = map(glob.glob, patts)     # name matches in this dir only

  20.     count = 0
  21.     for list in filelists:
  22.         for fname in list:
  23.             if listonly:
  24.                 print count+1, '=>;', fname
  25.             else:
  26.                 convertEndlines(sys.argv[1], fname)
  27.             count = count + 1

  28.     print 'Visited %d files' % count

复制代码


  1. ###################################################################
  2. # Use: "python fixeoln_one.py [tounix|todos] filename".
  3. # Convert end-of-lines in the single text file whose name is passed
  4. # in on the command line, to the target format (tounix or todos).  
  5. # The _one, _dir, and _all converters reuse the convert function
  6. # here.  convertEndlines changes end-lines only if necessary:
  7. # lines that are already in the target format are left unchanged,
  8. # so it's okay to convert a file >; once with any of the 3 fixeoln
  9. # scripts.  Notes: must use binary file open modes for this to
  10. # work on Windows, else default text mode automatically deletes
  11. # the \r on reads, and adds an extra \r for each \n on writes;
  12. # Mac format not supported; PyTools\dumpfile.py shows raw bytes;
  13. ###################################################################

  14. import os
  15. listonly = 0   # 1=show file to be changed, don't rewrite

  16. def convertEndlines(format, fname):                      # convert one file
  17.     if not os.path.isfile(fname):                        # todos:  \n   =>; \r\n
  18.         print 'Not a text file', fname                   # tounix: \r\n =>; \n
  19.         return                                           # skip directory names

  20.     newlines = []
  21.     changed  = 0
  22.     for line in open(fname, 'rb').readlines():           # use binary i/o modes
  23.         if format == 'todos':                            # else \r lost on Win
  24.             if line[-1:] == '\n' and line[-2:-1] != '\r':
  25.                 line = line[:-1] + '\r\n'
  26.                 changed = 1
  27.         elif format == 'tounix':                         # avoids IndexError
  28.             if line[-2:] == '\r\n':                      # slices are scaled
  29.                 line = line[:-2] + '\n'
  30.                 changed = 1
  31.         newlines.append(line)

  32.     if changed:
  33.         try:                                             # might be read-only
  34.             print 'Changing', fname
  35.             if not listonly: open(fname, 'wb').writelines(newlines)
  36.         except IOError, why:
  37.             print 'Error writing to file %s: skipped (%s)' % (fname, why)

  38. if __name__ == '__main__':
  39.     import sys
  40.     errmsg = 'Required arguments missing: ["todos"|"tounix"] filename'
  41.     assert (len(sys.argv) == 3 and sys.argv[1] in ['todos', 'tounix']), errmsg
  42.     convertEndlines(sys.argv[1], sys.argv[2])
  43.     print 'Converted', sys.argv[2]

复制代码


copy to a floder
execute python fixeoln_dir.py [tounix|todos]

论坛徽章:
0
3 [报告]
发表于 2004-09-08 12:43 |只看该作者

关于DOS2UNIX的问题(批量转换)

hehe...

论坛徽章:
0
4 [报告]
发表于 2004-09-08 13:41 |只看该作者

关于DOS2UNIX的问题(批量转换)

谢谢!我试一下;

论坛徽章:
0
5 [报告]
发表于 2004-09-08 14:47 |只看该作者

关于DOS2UNIX的问题(批量转换)

不行啊;只能改变当前目录的;不能改变子目录的;
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP