86 votes
Accepted

Delete folder if it exists in PowerShell

I assume you're just trying to avoid the error message in case it doesn't exist. What if you just ignore it: Remove-Item $folder -Recurse -ErrorAction Ignore If ...
  • 1,759
48 votes

A file class for Python

I'm about to say a lot of fairly unkind-sounding things. I'll say upfront, though, that this is generally good, simple, well-documented code. Unfortunately, it makes a pretty serious mistake that I ...
  • 840
18 votes
Accepted

Path shortener for GUI application

Python is 'batteries included', this means offloading code to the core library is a good thing, and often possible. In this case, if you're running Python 3.4+ you could make use of ...
  • 41.7k
14 votes
Accepted

Powershell search millions of files as fast as possible

First, you don't need to call Get-Date for every file. Just call it once at the beginning: ...
  • 1,663
14 votes
Accepted

Simple backup script in Python

This program is really a pleasure to read. It accomplishes the desired outcome neatly and succinctly. The only thing I could think of that might be an improvement is threading. Other than that this ...
  • 2,511
14 votes

A file class for Python

Private members You should mark file_path as not part of the public API for the class. You accomplish this with a leading underscore in the member name. eg) ...
  • 32.1k
13 votes

A file class for Python

One more warning bell here. You should not perform any shell invocation unless absolutely necessary, and that comes with pitfalls and security considerations that have to be fully understood. You ...
  • 5,607
12 votes
Accepted

Adding data from multiple Excel files to two lists

Your problem is not with 5 (or more) loops. Your problem is that you mixing code of different nature: walking over filesystem and name matching, and processing of files into single chunk of code. ...
  • 236
11 votes
Accepted

Minimalistic rmdir

Style Minor nitpicking: your style is not 100% consistent. ...
  • 406
11 votes
Accepted

Copy directories while changing Unicode filenames to ASCII

My overall take was "Nice!". :) Given your exchange with Patrick J S above (not to mention my lack of familiarity with the wrinkles discussed in the relevant RFCs) I'm not going to comment at all on ...
  • 381
11 votes
Accepted

Bash script that lowercases files

In my testing, constructing one Perl script and running it repeatedly is much faster (0.5s versus 3.6s) then running a new Perl instance for each replacement: ...
  • 1,338
11 votes
Accepted

Generate a nested structure based on a list of file paths

You can split each path at / using split and then use reduce() to build that object. <...
11 votes
Accepted

A minimal CLI password storage and retrieval manager

I would pinpoint the major problems in the code which I would address first: do not manually handle provided command-line arguments - there is a great built-in module for it - ...
  • 17.1k
11 votes
Accepted

A utility to swap two files

file1_path == file2_path surely tells that paths refer to the same file. However, even if file1_path != file2_path they still ...
  • 54.1k
10 votes
Accepted

List files in a directory

You do not use file_list Emptiness of container (as in PEP8) is usually checked by if not container. For path operations ...
10 votes

Synchronize multiple folders

I don't have time for a full review at the moment but something filled me with rage jumped out at me... Naming The code should be readable - you have 2 ways of making that the case, one is by naming ...
  • 16.7k
10 votes
Accepted

Filtering log files from a server

Raising useful errors If you have something like this: raise You're doing something wrong. Just calling raise with no ...
10 votes
Accepted

System backup on Linux

First of all, you should definitely read the Python style guide (PEP0008). It contains tons of good formatting and style info to make code more readable and neat for you and other users. I'll ...
10 votes
Accepted

Simple extractor for Quake-2 PAK archives

A common problem with file archive extractors is vulnerability to directory traversal attacks. A maliciously crafted archive could contain an entry with a path like ...
10 votes

Get Workbook Method(s)

I will not mention again that I find the outdenting of Dim statements hurt readability. Instead I'll focus on this part: ...
10 votes

Get directory permissions for all directories in tree

This is not efficient pathString += String is immutable so it builds a new string every time Use StringBuilder
  • 5,930
10 votes

Removing multiple slashes from path

Here are some suggestions for how you might improve your code. Use the appropriate #includes This program fragment requires headers, which should be included: <...
  • 64.6k
10 votes

Backup with Hanoi Strategy

Do not silently ignore invalid arguments Your script allows to specify the source directory (and other locations). Here is the relevant part in function parseArgs()...
  • 22.6k
10 votes
Accepted

Go function to test whether a file exists

Instead of checking with an if statement if errStat is null and then returning false: ...
10 votes

A file class for Python

At the moment, your class only fully works on Windows. On Mac OS and Linux all methods that call a command line tool with os.system will fail. You should make this ...
  • 40.6k
9 votes

Implementation of the ls command with several options

Looks pretty clean for the most part! Here's the source of the actual GNU implementation used on most systems for comparison (if you already haven't taken a look at it). Just a few notes: Looks ...
  • 21.7k
9 votes

Removing multiple slashes from path

Your specification has holes in it. What will be if user passes NULL? Undefined behavior? Return? What if the path is already valid? Copy the string? Return ...
  • 9,171
9 votes
Accepted

Recursively list files within a directory

Your function could be a generator (which were introduced in Python 2.2). It should also expose the pattern for the filter as a parameter: ...
  • 40.6k
9 votes
Accepted

Retrieve filetype:pdf from library

Similarly to a feature in most UNIX shells, Python has a globbing module, called glob. It even supports the extended globbing (using ...
  • 40.6k

Only top scored, non community-wiki answers of a minimum length are eligible