Location current working directory in Colab - location

can anybody help me to find the current working directory in Colab. I am working on a project, which has a folder containing multiple sub-folders. When I need to read a text or csv file (in the same directory as nb) I have to locate my current notebook first which is quite tedious. I tried
%pwd
And also tried
import os
os.path
The output for first one is simply
/content
While os.path does not output anything

/content is where your notebook is, why you have to find it? Take example of a test.txt if it was in files. You can point to text file as /content/test.txt

Related

Stata can't open file saved in local cloud storage folder on Mac

I am using Stata on a Mac. I updated the OS to Monterey today. If I try to open a .dta file---that is saved on my local computer in a folder that syncs to OneDrive---from code in the Do File Editor/Command prompt (where I refer to the file using its full file path), it doesn't open. The error is r(601) - File ... cannot be found.
But if I click on it in Finder, it opens in Stata just fine. After I have done this, I can then open this file in Stata by running the exact same Stata code that didn't work before. Somehow it seems like Stata can't see the file/folder initially, but then it can. I am not sure if this has to do with the OneDrive file/folder permissions or something else.
If, in Stata, I change the directory to the correct directory using cd and then load the file (without referring to the path), it works. However, I would like to be able to refer to the file using the entire file path and not have to change the directory each time.
Interestingly, if I change the directory to the correct directory using Change working directory in the File drop down and then try to load the file using the file path, it also works. But if I do not do the drop down and write out the cd using code, and try accessing the file using the path it doesn't work!
Somehow Stata has to initially "see"/"access" the correct folder (in a very particular way) before being able to access it with the file path. Very strange. (Just to be clear, if I do not change the directory to the correct directory and then try to load the file using its file path, it doesn't work.)
I also tried creating a file using texdoc init... (in a folder that already exists, but that has not been used by Stata before) and get this error:
could not create directory .....
mkdir(): 693 could not create directory
texdoc_mkdir(): - function returned error
texdoc_init(): - function returned error
<istmt>: - function returned error
I do not get this error if I instead use another folder in the same directory that I have previously used.
I think that the error I am encountering is the same or similar to this one (with no acceptable answer): https://answers.microsoft.com/en-us/msoffice/forum/all/onedrive-folder-doesnt-exist/e6b97d47-3f6a-4863-bf68-d3a02832f2bb

How to zip a folder in MacOS without creating an extra directory inside the zip file

I got a .zip file from my friend and it was compressed under windows, which contains three subfolders inside of it, and when I check the contents of it on my Mac terminal it looks like this:
Now I unzip this file and then zip it through terminal, and when I check the contents of that zip file it becomes like this:
I have googled how to zip on mac without creating a subfolder with same name but none of them solves the problem, my question is how to do the zip on Mac which makes the zip file looks exactly same with the initial one I got.
Thanks very very much
New edit:
I think I might did not do very well to summarize my problem, so the initial folder contains three sub-folders and all of them were created in windows environment and compressed on windows, when I tried to unzip it on my MacOS machine, the unzipped folder looks still good but when I do the compression on Mac and then view the .zip file through unzip -l xxxx.zip, it is giving me 6 files in which the three sub-folders are also treated as files. Based on my knowledge this is because in BSD systems all the folders are treated as files but in windows they are not, what I'm currently doing is to delete all these files that represent folders through "zip -d", which I know is very silly. I would be more than happy to talk about this from an operating system view with anybody who is interested in it. Thanks in advance.
For me this command works fine:
zip -j zippedFolder.zip myFolder/*
To unzip I used
unzip zippedFolder.zip
and I've got only the data from the folder.
Example: The folder I want to zip is on the desktop and he's called testFolder.
Open Terminal
cd /Users/yourUser/Desktop
zip -f myZip.zip testFolder/*

os.listdir() showing file actually not in the folder on Python and Windows

The following Python script:
import os
print os.listdir('D:\images')
is outputing the names of all the folders in the D:\image directory, but it is also showing desktop.ini in the same folder, while there is no such file in the image directory.
Its also not a hidden item, I am sure of that.
Why is it then showing it as a content?
desktop.ini is a protected system file, and Windows tends to hide it.
You can verify by going to D:\images in a terminal and running dir /A.
See this answer as well.
You can use os.walk() if you want more control, it will give you directories and files separately. You can also use os.path.isdir() to find out if an entry you get is a directory.
I used os.path.isdir(path) to check whether the item returned by os.listdir() is a directory or not
This way,desktop.ini didn't meet the criteria of being a directory and my program scanned all the folders.

Import Query into Windows Grep tool

I've been using grepWin,
And I would like to somehow perform a series of queries for pdf links within .html files.
Thus far with the tool I have been using I just input each individual PDF name and copy the file paths of each reference.
This works fine but I have several hundred specific PDFs I need to find the references for,
And I was wondering if this was possible by using Cygwin or some other cmdline like Findstr to pipe a textfile of links to PDF's which I am searching.
I will give an example:
Spring-Summer.pdf
I would copy all of the paths to which the listed file is linked to within html files.
I then need that copied next to it, or in its own column within csv.
I'm not sure if it's at all probable anyone has asked this before. Currently I'm filling out a spreadsheet of links to these files for a website..
In Linux the following command will find all the html files which contain the specified string:
grep -Rl "Spring-Summer.pdf" <some root folder>
The -R option is to search recursively, and -l is to display just the file name without content.
The same should work on Cygwin.

Setting filepaths inside your .rb file when converted into an exe with ocra

I've had a lot of trouble getting one my wxruby scripts to use an image file I included in the exe with ocra. If I didn't have the original image file in the same directory as the exe, the exe wouldn't find the image.
shape = File.join('warning3.png' )
I wanted the script to find the image I included in the exe.
In the ocra documentation it mentions Dir.chdir File.dirname($0) but I didn't get what it meant when it was mentioned.
If you need to use say an image file in your script you can make sure it works merely by having this Dir.chdir(File.dirname($0)) line before you try to use any of your images.
For example in my app I'm using an image file and I couldn't get my exe to work if it wasn't in the same dir as the file but with the below it works anywhere so far.
Dir.chdir(File.dirname($0))
shape = File.join('warning3.png' )
I think this sets the script's current directory to the exe's directory inside it? If I'm wrong please let me know!

Resources