471,044 Members | 951 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Loading a file through python using Windows

kuhey
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()

1 1754
bvdet
2,851 Expert Mod 2GB
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.

Similar topics

2 posts views Thread by Roose | last post: by
reply views Thread by Ishwor | last post: by
1 post views Thread by Srijit Kumar Bhadra | last post: by
17 posts views Thread by Paul Rubin | last post: by
3 posts views Thread by Stanislaw Findeisen | last post: by
8 posts views Thread by mistral | last post: by
4 posts views Thread by est | last post: by

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.