Reading a file using a relative path in a Python project

Relative paths are relative to current working directory. If you do not want your path to be relative, it must be absolute. But there is an often used trick to build an absolute path from current script: use its __file__ special attribute: from pathlib import Path path = Path(__file__).parent / “../data/test.csv” with path.open() as f: … Read more

Read and Write Text in ANSI format

To read a text file with a specific encoding you can use a FileInputStream in conjunction with a InputStreamReader. The right Java encoding for Windows ANSI is Cp1252. reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), “Cp1252”)); To write a text file with a specific character encoding you can use a FileOutputStream together with a OutputStreamWriter. writer … Read more

Force “git status” to output color on the terminal (inside a script)

To avoid changing your git config, you can enable colour just for the current command by passing a config variable with -c. For the status command, the variable is color.status: git -c color.status=always status | less -REX For diff, show, log and grep commands, the variable is color.ui: git -c color.ui=always diff | less -REX … Read more

Converting IO Int to Int

As a general rule you write something like this: do x <- somethingThatReturnsIO somethingElseThatReturnsIO $ pureFunction x There is no way to get the “Int” out of an “IO Int”, except to do something else in the IO Monad. In monad terms, the above code desugars into somethingThatReturnsIO >>= (\x -> somethingElseThatReturnsIO $ pureFunction x) … Read more

java read file from network device

The simplest way to do this would be to read it using regular file paths. On Windows: new File(“\\\\server\\path\\to\\file.txt”) // (double-backslashes required for backslashes in path) On Unix: First mount the share using Samba (SMB, NFS or whatever other protocol) to some location like /mnt/network. Then you can use: new File(“/mnt/network/path/to/file.txt”) Once you have the … Read more

Add a new line at a specific position in a text file.

This will add the line where you want it. (Make sure you have using System.IO; and using System.Linq; added) public void CreateEntry(string npcName) //npcName = “item1” { var fileName = “test.txt”; var endTag = String.Format(“[/{0}]”, npcName); var lineToAdd = “//Add a line here in between the specific boundaries”; var txtLines = File.ReadAllLines(fileName).ToList(); //Fill a list … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)