How to change directorys inside a python script/batch file (DOS)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gpbajaj
    New Member
    • Jul 2007
    • 1

    #1

    How to change directorys inside a python script/batch file (DOS)

    Hi I was wondering if anybody knows how to change directories within a python/script file from DOS
    I have Windows XP, but prefer running from DOS


    So i would executre like this
    python filebelow.py

    *************** *************** *************** ********
    [CODE=python]
    import urllib
    import os

    def saveUrlToFile( url, fileName ):



    f = open( fileName, 'wb' )
    data = urllib.urlopen( url).read()
    f.write( data )
    f.close()

    saveUrlToFile(

    'www.google.ca' ,
    'googlepage.txt ' )
    [/CODE]
    Here is where I want to change to another directory, for example
    cd\
    cd newdirectory

    [CODE=python]
    saveUrlToFile(

    'www.hotmail.ca ',
    'hotmailpage.tx t' )[CODE]



    *************** *************** ***
    Last edited by bartonc; Jul 3 '07, 07:34 AM. Reason: Added [CODE=python][CODE] tags.
  • ghostdog74
    Recognized Expert Contributor
    • Apr 2006
    • 511

    #2
    Originally posted by gpbajaj
    Hi I was wondering if anybody knows how to change directories within a python/script file from DOS
    I have Windows XP, but prefer running from DOS


    So i would executre like this
    python filebelow.py

    *************** *************** *************** ********

    import urllib
    import os

    def saveUrlToFile( url, fileName ):



    f = open( fileName, 'wb' )
    data = urllib.urlopen( url).read()
    f.write( data )
    f.close()

    saveUrlToFile(

    'www.google.ca' ,
    'googlepage.txt ' )

    Here is where I want to change to another directory, for example
    cd\
    cd newdirectory

    saveUrlToFile(

    'www.hotmail.ca ',
    'hotmailpage.tx t' )



    *************** *************** ***
    check the os module for chdir(). This is the method you use for changing directories from within Python

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      You can also give the file name an explicit path:[code=Python]
      saveUrlToFile(' www.hotmail.ca' ,'C:\\newdirect ory\\hotmailpag e.txt')[/code]

      Comment

      Working...