Search and replace a line in a file in Python

The shortest way would probably be to use the fileinput module. For example, the following adds line numbers to a file, in-place: import fileinput for line in fileinput.input(“test.txt”, inplace=True): print(‘{} {}’.format(fileinput.filelineno(), line), end=”) # for Python 3 # print “%d: %s” % (fileinput.filelineno(), line), # for Python 2 What happens here is: The original file … Read more

How do I load a file from resource folder?

Try the next: ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream(“test.csv”); If the above doesn’t work, various projects have been added the following class: ClassLoaderUtil1 (code here).2 Here are some examples of how that class is used: src\main\java\com\company\test\YourCallingClass.java src\main\java\com\opensymphony\xwork2\util\ClassLoaderUtil.java src\main\resources\test.csv // java.net.URL URL url = ClassLoaderUtil.getResource(“test.csv”, YourCallingClass.class); Path path = Paths.get(url.toURI()); List<String> lines = Files.readAllLines(path, … Read more

How to restore the permissions of files and directories within git if they have been modified?

Git keeps track of filepermission and exposes permission changes when creating patches using git diff -p. So all we need is: create a reverse patch include only the permission changes apply the patch to our working copy As a one-liner: git diff -p -R –no-ext-diff –no-color \ | grep -E “^(diff|(old|new) mode)” –color=never \ | … Read more

Nested using statements in C#

The preferred way to do this is to only put an opening brace { after the last using statement, like this: using (StreamReader outFile = new StreamReader(outputFile.OpenRead())) using (StreamReader expFile = new StreamReader(expectedFile.OpenRead())) { ///… }