473,574 Members | 3,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

1 New Member
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
  4. def saveUrlToFile( url, fileName ):
  8.     f = open( fileName, 'wb' )
  9.     data = urllib.urlopen(url).read()
  10.     f.write( data )
  11.     f.close()
  13. saveUrlToFile(
  15. 'www.google.ca',
  16. 'googlepage.txt' )
Here is where I want to change to another directory, for example
cd newdirectory


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

*************** *************** ***
Jul 3 '07 #1
2 2214
511 Recognized Expert Contributor
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 )


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

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


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

*************** *************** ***
check the os module for chdir(). This is the method you use for changing directories from within Python
Jul 3 '07 #2
2,851 Recognized Expert Moderator Specialist
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

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

Similar topics

by: Jeff Wagner | last post by:
I have a project of converting numerous DOS cmd shell scripts to Python. Is there a tutorial to getting started? Thanks, Jeff
by: SeeBelow | last post by:
I have not been able to find a simple way to have a python script execute an MS-DOS program repeatedly. execv(), for example, won't run it more than once. I have been using many lines of a .bat file, like so: .. .. .. del xyz myexec 1 2
by: ryanmhuc | last post by:
I know the subject might be confusing. I am no beginner with javascript but I haven't been able to figure out how to get the javascript file name from code inside the file. So you have an HTML doc with script tag who's source is a javascript file. <HTML> <script src="javascript.js"></script> </HTML> Javascript.js
by: n o s p a m p l e a s e | last post by:
Suppose I have a batch file called mybatch.bat and I want to run it from a python script. How can I call this batch file in python script? Thanx/NSP
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.