Using the following code:
fileName = 'Data\\all_earthquakes.csv'
with open(fileName, 'rb') as csv_file:
attrHeaderRow = csv_file.readline().strip()
I get the following error:
IOError: [Errno 2] No such file or directory: 'Data\\all_earthquakes.csv'
Works perfectly fine on my Windows 7 machine.
解决方案
Windows and Mac OS X use different characters to separate elements in paths. Windows uses the backslash, Mac OS X (and Linux/UNIX) uses the forward slash. Python takes care of this for you: use os.path.join to build paths using the correct separator for the current operating system or use os.sep if you need the actual character that is used for path separation.
import os
import sys
fileName = os.path.join('Data', 'all_earthquakes.csv')
print('Directory separator on your platform ({}): {}'.format(sys.platform, os.sep))
Note that Windows generally accepts the forward slash as path separator as well when using Windows APIs - it's just CMD.EXE that does not accept them. That's why on Windows, os.altsep is set to the forward slash (and people just use the forward slash in all paths, even on Windows).