python中可恶的回车符把俺折腾了一天

超级网迷+ 电脑迷+ 音乐迷
打印 被阅读次数

这两天练习写了个python脚本,就是在命令行下扫描指定的驱动器或者文件夹,看有哪些文件很久都没有被访问过了。可以是天数,或者指定的日期以后。

但是调试的时候,总是报错:

"FScan.py", line 115, in
main(sys.argv[1])
line 43, in main access_time = time.mktime(time.strptime(entry, "%Y/%m/%d"))
line 313, in strptime data_string(found.end():])
ValueError: unconverted data remains:

呵呵~~折腾了我差不多一个周末的时间才搞明白。原来是python把我输入的日期字符串,后面加了个r。
比如日期“2002/01/01”,编译运行的时候就变成了“2002/01/01r"。网上称之为 “python中可恶的回车符 ”。也就是,在python中,使用文件输入流时,每读入一行总会带着可恶的结尾回车符。把它去掉后,果然不报错了。

俺是python的粉丝而已,希望与朋友们交流。

源码下载

登录后才可评论.
Logo

Looks like your ad blocker is on.

×

We rely on ads to keep creating quality content for you to enjoy for free.

Please support our site by disabling your ad blocker.

Continue without supporting us

Choose your Ad Blocker

  • Adblock Plus
  • Adblock
  • Adguard
  • Ad Remover
  • Brave
  • Ghostery
  • uBlock Origin
  • uBlock
  • UltraBlock
  • Other
  1. In the extension bar, click the AdBlock Plus icon
  2. Click the large blue toggle for this website
  3. Click refresh
  1. In the extension bar, click the AdBlock icon
  2. Under "Pause on this site" click "Always"
  1. In the extension bar, click on the Adguard icon
  2. Click on the large green toggle for this website
  1. In the extension bar, click on the Ad Remover icon
  2. Click "Disable on This Website"
  1. In the extension bar, click on the orange lion icon
  2. Click the toggle on the top right, shifting from "Up" to "Down"
  1. In the extension bar, click on the Ghostery icon
  2. Click the "Anti-Tracking" shield so it says "Off"
  3. Click the "Ad-Blocking" stop sign so it says "Off"
  4. Refresh the page
  1. In the extension bar, click on the uBlock Origin icon
  2. Click on the big, blue power button
  3. Refresh the page
  1. In the extension bar, click on the uBlock icon
  2. Click on the big, blue power button
  3. Refresh the page
  1. In the extension bar, click on the UltraBlock icon
  2. Check the "Disable UltraBlock" checkbox
  1. Please disable your Ad Blocker
  2. Disable any DNS blocking tools such as AdGuardDNS or NextDNS

If the prompt is still appearing, please disable any tools or services you are using that block internet ads (e.g. DNS Servers).

Logo