471,594 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

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

************************************************** ***
Expand|Select|Wrap|Line Numbers
  1. import urllib
  2. import os
  3.  
  4. def saveUrlToFile( url, fileName ):
  5.  
  6.  
  7.  
  8.     f = open( fileName, 'wb' )
  9.     data = urllib.urlopen(url).read()
  10.     f.write( data )
  11.     f.close()
  12.  
  13. saveUrlToFile(
  14.  
  15. 'www.google.ca',
  16. 'googlepage.txt' )
  17.  
Here is where I want to change to another directory, for example
cd\
cd newdirectory

[code=python]
saveUrlToFile(

'www.hotmail.ca',
'hotmailpage.txt' )[code]



*********************************
Jul 3 '07 #1
2 2029
ghostdog74
511 Expert 256MB
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.txt' )



*********************************
check the os module for chdir(). This is the method you use for changing directories from within Python
Jul 3 '07 #2
bvdet
2,851 Expert Mod 2GB
You can also give the file name an explicit path:
Expand|Select|Wrap|Line Numbers
  1. saveUrlToFile('www.hotmail.ca','C:\\newdirectory\\hotmailpage.txt')
Jul 3 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.