Python list directory, subdirectory, and files

Use os.path.join to concatenate the directory and file name: for path, subdirs, files in os.walk(root): for name in files: print(os.path.join(path, name)) Note the usage of path and not root in the concatenation, since using root would be incorrect. In Python 3.4, the pathlib module was added for easier path manipulations. So the equivalent to os.path.join … Read more

Rename a file using Java

Copied from http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html // File (or directory) with old name File file = new File(“oldname”); // File (or directory) with new name File file2 = new File(“newname”); if (file2.exists()) throw new java.io.IOException(“file exists”); // Rename file (or directory) boolean success = file.renameTo(file2); if (!success) { // File was not successfully renamed } To append to … Read more

How do I move files in node.js?

According to seppo0010 comment, I used the rename function to do that. http://nodejs.org/docs/latest/api/fs.html#fs_fs_rename_oldpath_newpath_callback fs.rename(oldPath, newPath, callback) Added in: v0.0.2 oldPath <String> | <Buffer> newPath <String> | <Buffer> callback <Function> Asynchronous rename(2). No arguments other than a possible exception are given to the completion callback.

How can I tell how many objects I’ve stored in an S3 bucket?

Using AWS CLI aws s3 ls s3://mybucket/ –recursive | wc -l or aws cloudwatch get-metric-statistics \ –namespace AWS/S3 –metric-name NumberOfObjects \ –dimensions Name=BucketName,Value=BUCKETNAME \ Name=StorageType,Value=AllStorageTypes \ –start-time 2016-11-05T00:00 –end-time 2016-11-05T00:10 \ –period 60 –statistic Average Note: The above cloudwatch command seems to work for some while not for others. Discussed here: https://forums.aws.amazon.com/thread.jspa?threadID=217050 Using AWS Web … Read more

How to modify a text file?

Unfortunately there is no way to insert into the middle of a file without re-writing it. As previous posters have indicated, you can append to a file or overwrite part of it using seek but if you want to add stuff at the beginning or the middle, you’ll have to rewrite it. This is an … Read more

How to save a dictionary to a file?

Python has the pickle module just for this kind of thing. These functions are all that you need for saving and loading almost any object: with open(‘saved_dictionary.pkl’, ‘wb’) as f: pickle.dump(dictionary, f) with open(‘saved_dictionary.pkl’, ‘rb’) as f: loaded_dict = pickle.load(f) In order to save collections of Python there is the shelve module.

Why does Path.Combine not properly concatenate filenames that start with Path.DirectorySeparatorChar?

This is kind of a philosophical question (which perhaps only Microsoft can truly answer), since it’s doing exactly what the documentation says. System.IO.Path.Combine “If path2 contains an absolute path, this method returns path2.” Here’s the actual Combine method from the .NET source. You can see that it calls CombineNoChecks, which then calls IsPathRooted on path2 … Read more