3 ms·
Python one-liner to compare two files (conditions apply)
- pklausler 11y agoOr do it in a language with lazy I/O and don't worry so much about memory requirements. import Data.ByteString.Lazy as LBS areFilesEqual x y = (==) <$> LBS.readFile x <*> LBS.readFile y (EDIT: added line break)
- ascotan 11y agoor you could just: diff X Y and check the return code. Oh you crazy pythonistas.
- vram22 11y agoYou're either trolling, or just didn't / couldn't get that the post was for fun and learning, exploring new/alternative ways of doing things. > diff X Y For that matter, doing: cmp X Y is even simpler since it doesn't create as much output as diff when the files are different. And hey, we saved one letter over diff. /s According to your logic, no one should have done an alternative version of a program after the first one for that need was created. The world would be a much poorer place. Only one language, assembly or FORTRAN /COBOL, only one OS, one DB, etc. Poor fellow - you crazy commenter.
- 13of40 11y agofunction cmp($l, $r) {"$(gc $l)" -eq "$(gc $r)"} Though, in real life you should compare the lengths of the files before reading them.
- vram22 11y agoI do that in my program - compare lengths first. There's also an interesting comment there by a reader about how filecmp (which can do the job) uses stat info with a cache. What language is that? shell? and what is gc?
- 13of40 11y agoPowerShell. GC is Get-Content. (It's actually horribly inefficient because it tags every line read from the file with half a dozen metadata properties, but it looks good in one-liners.)