By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,825 Members | 1,061 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,825 IT Pros & Developers. It's quick & easy.

Loading a file through python using Windows

kuhey
P: 1
Hello! Recently I've decided to try something new and started watching the google tutorials for python (http://www.youtube.com/watch?v=haycL41dAhg) ... most of it was very clear and I've learned a lot... but since I am using a Win 7 and the man presenting is using a Mac, there were some difficulties... so my question here is... how do I start a .py program that takes a .txt file (eg. has a function to count the words in there)? In the video he just writes it into the terminal.. I'm thinking command prompt.. but I'm very confused :( Thanks in advance (sorry if it's hard to understand, I'm not a native english speaker)
Jan 6 '13 #1

✓ answered by bvdet

Open your command prompt. Change your current directory to the directory containing your script and data file, or you will need to enter the full path and file names. This assumes your python executable is on the system path.

At the command prompt, which may look something like "C:\Python27_64>" enter:
python script_name.py data_file_name.txt
where "script_name.py" is the file name of your script and "data_file_name.txt" is the file name of your data file. In the example below, sys.argv[1] will be the file name of your data script. sys.argv[0] is the name of the python script.
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. print sys.argv
  4. print open(sys.argv[1]).read()

Share this Question
Share on Google+
1 Reply

bvdet
Expert Mod 2.5K+
P: 2,851
Open your command prompt. Change your current directory to the directory containing your script and data file, or you will need to enter the full path and file names. This assumes your python executable is on the system path.

At the command prompt, which may look something like "C:\Python27_64>" enter:
python script_name.py data_file_name.txt
where "script_name.py" is the file name of your script and "data_file_name.txt" is the file name of your data file. In the example below, sys.argv[1] will be the file name of your data script. sys.argv[0] is the name of the python script.
Expand|Select|Wrap|Line Numbers
  1. import sys
  2.  
  3. print sys.argv
  4. print open(sys.argv[1]).read()
Jan 6 '13 #2

Post your reply

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