[Tutor] os.chdir() will not accept string variable
Rodney Lewis
therealdotcomboy at gmail.com
Sat Apr 16 03:00:38 CEST 2011
I cannot get os.chdir() to accept inputData[0]. os.chdir() works as
expected in the interpreter when I put the little 'r' before the exact
same string but as a literal, e.g.: r"F:\Music\Siouxsie and the
Banshees\the rapture"
When I try to run it I get the following error in reference to the
os.chdir() line:
WindowsError: [Error 123] The filename, directory name, or volume
label syntax is incorrect 'F:\\Music\\Siouxsie and the Banshees\\the
rapture\n'
Why is it doubling the backslashes and adding '\n' to the end? How do
I make it stop? What does the little 'r' mean before a string literal
and how do I do the same for a string variable?
# mdf -- mp3datafixer
import glob, os
def mdf():
inputFile = open( 'mdfinputs.txt', 'r' )
inputData = inputFile.readlines()
inputFile.close()
os.chdir( r'%s' % inputData[0] )
newNames = []
oldNames = glob.glob( '*.*' )
for index, item in enumerate( oldNames ):
print index, item
if __name__ == '__main__':
mdf()
raw_input( "\nPress 'enter' to close console window:" ) # Keeps
console window open in Windows
Thanks!
--
Rodney Lewis
Please Visit My Homepage:
http://www.squidoo.com/dotcomboy
More information about the Tutor
mailing list