[Tutor] Renaming Files in Directory
Hello, I have the following program import os path = '/Users/felishalawrence/testswps/vol1' for file in os.listdir(path): newFile = path+file[:file.rindex("v")]+"v20" print newFile and I want to output the results of the 'newFile' variable into the directory specified by the 'path' variable. There are current files in this directory, but I would like tho replace them with different names. Can anyone point me in the right direction? Thanks, Felisha Lawrence -- Felisha Lawrence Howard University Program for Atmospheric Sciences(HUPAS), Graduate Student NASA URC/BCCSO Graduate Fellow NOAA NCAS Graduate Fellow Graduate Student Association for Atmospheric Sciences(GSAAS), Treasurer (240)-535-6665 (cell) felisha.lawre...@gmail.com (email) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Renaming Files in Directory
Felisha Lawrence wrote: > I have the following program > import os > > path = '/Users/felishalawrence/testswps/vol1' > for file in os.listdir(path): > newFile = path+file[:file.rindex("v")]+"v20" > > print newFile > and I want to output the results of the 'newFile' variable into the > directory specified by the 'path' variable. There are current files in > this directory, but I would like tho replace them with different names. > Can anyone point me in the right direction? So you can no longer defer that dreadful task? ;) To rename a file you need its old and its new name, both preferrably with their complete path, for example for old_name in os.listdir(path): old_file = os.path.join(path, old_name) new_name = old_name[:old_name.rindex("v")] + "v20" new_file = os.path.join(path, new_name) os.rename(old_file, new_file) If there are only files containing a "v" in your /Users/felishalawrence/testswps/vol1 folder the above should already work. Here are a few ideas to make it more robust (or rather less brittle): Verify that old_name contains a "v" using the `in` operator Verify that old_file is a file using os.path.isfile() Verify that new_file doesn't exist with os.path.exists() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Renaming Files in Directory
On 09/10/14 00:58, Felisha Lawrence wrote: Hello, I have the following program import os path = '/Users/felishalawrence/testswps/vol1' for file in os.listdir(path): newFile = path+file[:file.rindex("v")]+"v20" print newFile and I want to output the results of the 'newFile' variable into the directory specified by the 'path' variable. You want the os.rename function. Also you should use os.path.join() to create the path rather than string addition. It will ensure the correct separators are used for the OS. You might also want to look at glob.glob() rather than listdir to get a listing of files matching a wildcard pattern. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] alternative Python 2.6 install?
Hi, I need to install Python 2.6 on my Debian system to check some code.*) What is the easiest way to do this? Simply "sudo apt-get install python2.6"? I know I can also compile it and then do make altinstall, but I prefer apt-get. I am kinda paranoid that I might accidentally change my system Python version. Thank you! Regards, Albert-Jan *) albertjan@debian:~$ uname -a Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u3 x86_64 GNU/Linux albertjan@debian:~$ python -c "import sys; print sys.version_info" sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0) ~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
I'm new to programming. Started reading the book 'How to think like a computer Scientist-learning with python'. I'm now in chapter 3 sub-chapter 3.4 Math functions. When I write the following code: import maths; decibel = math.log10 (17.0); angle = 1.5; height = math.sin(angle); print height; I get the following error: Traceback (most recent call last): File "C:/Python27/test", line 1, in import maths; ImportError: No module named maths I don't know what I'm doing wrong? >From what I've read the maths module is supposed to come with the python installation package. I'm using a windows 8 operating system python 2.7.8 please help? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On Oct 9, 2014 8:00 PM, "William Becerra" wrote: > > I'm new to programming. Started reading the book 'How to think like a computer Scientist-learning with python'. I'm now in chapter 3 sub-chapter 3.4 Math functions. > > When I write the following code: > > import maths; import math You added s > decibel = math.log10 (17.0); > angle = 1.5; > height = math.sin(angle); > print height; > > I get the following error: > > Traceback (most recent call last): > File "C:/Python27/test", line 1, in > import maths; > ImportError: No module named maths > > I don't know what I'm doing wrong? > From what I've read the maths module is supposed to come with the python installation package. > I'm using a windows 8 operating system > python 2.7.8 > please help? > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Hi there and welcome! import maths; decibel = math.log10 (17.0); angle = 1.5; height = math.sin(angle); print height; Traceback (most recent call last): File "C:/Python27/test", line 1, in import maths; ImportError: No module named maths Oops! It's a nice error report, though! Python tried to locate a module called 'maths' and was not able to find it. What happens if you try: import math N.B. You say 'import maths'--assume that this import succeeded. A few lines later, there's a line 'math.log10(17.0)' which seems to be trying to use something from a module called 'math' not 'maths'. I don't know what I'm doing wrong? Computers are so picky. From what I've read the maths module is supposed to come with the python installation package. The 'math' library is a standard library module for quite awhile now. Here's a possibly useful online link, which describes that module: https://docs.python.org/2/library/math.html This is just more documentation support, in addition to the book you are reading. I'm using a windows 8 operating system python 2.7.8 please help? One other issue I might point out. The semicolon at the end of the line (statement) is a feature of other programming languages with which you may be familiar (C, Java, Perl), but it is not necessary and, in fact, discouraged in Python. So, rid yourself of the semicolons and enjoy the benefits of a trivially cleaner syntax. Enjoy! -Martin P.S. Thanks for your clear question and letting us know your OS and Python version, as well. -- Martin A. Brown http://linux-ip.net/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
On 09/10/14 19:38, William Becerra wrote: import maths; Python, like most languages speaks American English so its math not maths. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Installing both Python 2.7 and Python 3.4 on Windows 7 Pro 64-bit: Install Python 2.7 FIRST!
I am hoping to save other people the grief I just worked through. I wanted to run both Python 2 and 3 on my windows PC, and, after googling this topic found that with Python 3.3 or later one could easily do both. So I merrily installed Python 3.4.2 first and then Python 2.7.8. A Python 3 program that had been working fine suddenly stopped working. After working down to a test portion of code that isolated the culprit I realized my Python 3 program was being interpreted by Python 2. I soon found that installing Python 2 first and then 3 enabled both to happily coexist. If there was a mention about the order of installation anywhere during my searches, I missed it. Anyway, I hope that my experience helps some other newbie who wants to play around with both major versions. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
It is working now. Thank you everyone. It was very helpfull. On Fri, Oct 10, 2014 at 2:36 AM, Alan Gauld wrote: > On 09/10/14 19:38, William Becerra wrote: > > import maths; >> > > Python, like most languages speaks American English > so its math not maths. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor