ruby / OS X cannot open existing file in iPhoto Library - ruby

env: OSX 10.10 / iPhoto 9.6 / Ruby 2.2
in a ruby script, I am trying to open an xml file from the 'iPhoto Library' to get the album list.. but I got an error :
f = File.open(#xmlpath)
Errno::ENOENT: No such file or directory # rb_sysopen - /Users/myself/Pictures/iPhoto%20Library/AlbumData.xml
first, I defined the 'iPhoto Library' path in my user path :
PhotoLib = File.expand_path(File.join("~","Pictures","iPhoto Library")
then I defined the #xml file path ( escaping the embedded spaces )
#xmlpath = URI.escape(File.join iPhotoLib, "AlbumData.xml")
finally I try to open the xml file
f = File.open(#xmlpath)
but it raises the 'No such file or directory' error... where am I wrong ?
the file exists in the "iPhoto Library" content ...

You shouldn't be using URI.escape - that is for urls but what you pass to File.open is a path on your local filesystem, not a url. In particular percentage escapes ("%20") don't make sense to your filesystem

I should use
library_path = Pathname.new(ENV["HOME"]) + "Pictures" + "iPhoto Library.photolibrary"
xml_path = library_path + "AlbumData.xml"
f = File.open(xml_path)
Pathname handle correctly it ....

Related

Bash Mac Terminal to Organize Filestructure

I have 12,000+ files that need to be organized. All the folders are included, however the files are in a flattened filestructure right now.
My folders and files are all named with the path that they should be in. For example, in one directory I have a folder named \textures and another folder named \textures\actors\bear however there is no \textures\actors folder. I am struggling at developing a macro that will take these folders and put them in the correct place that each folder and filename suggests it should be in. I would like to be able to automatically sort these into textures and inside that would be actors and inside that would be bear. However, there are over 12,000 files so I am looking for an automated process that will determine all of this and just do it if possible.
Is there a script that will look at every file or folder name and detect which folder the file or folder should be in in the directory and automatically move them there as well as create any folders that do not exist within the path given when needed?
Thanks
Given a directory structure like this:
$ ls /tmp/stacktest
\textures
\textures\actors\bear
fur.png
\textures\actors\bear\fur2.png
The python script below will turn it into this:
$ ls /tmp/stackdest
textures/actors/bear
fur.png
fur2.png
Python script:
from os import walk
import os
# TODO - Change these to correct locations
dir_path = "/tmp/stacktest"
dest_path = "/tmp/stackdest"
for (dirpath, dirnames, filenames) in walk(dir_path):
# Called for all files, recu`enter code here`rsively
for f in filenames:
# Get the full path to the original file in the file system
file_path = os.path.join(dirpath, f)
# Get the relative path, starting at the root dir
relative_path = os.path.relpath(file_path, dir_path)
# Replace \ with / to make a real file system path
new_rel_path = relative_path.replace("\\", "/")
# Remove a starting "/" if it exists, as it messes with os.path.join
if new_rel_path[0] == "/":
new_rel_path = new_rel_path[1:]
# Prepend the dest path
final_path = os.path.join(dest_path, new_rel_path)
# Make the parent directory
parent_dir = os.path.dirname(final_path)
mkdir_cmd = "mkdir -p '" + parent_dir + "'"
print("Executing: ", mkdir_cmd)
os.system(mkdir_cmd)
# Copy the file to the final path
cp_cmd = "cp '" + file_path + "' '" + final_path + "'"
print("Executing: ", cp_cmd)
os.system(cp_cmd)
The script reads all the files and folders in dir_path and creates a new directory structure under dest_path. Make sure you don't put dest_path inside dir_path.

File found with relative path but not absolute path? (VBS)

I am trying to launch a shortcut from VBScript, but I am running into a very odd error.
When I use the relative path of the shortcut, the script opens the shortcut perfectly fine. However, if I use the absolute file path (copied from windows explorer so no typos or anything like that) it gives me an error saying file not found.
Relative path code:
dim x
set x = CreateObject("WScript.shell")
x.Run("Shortcut.lnk")
set x = Nothing
This opens the file.
Absolute path code:
dim x
set x = CreateObject("WScript.shell")
x.Run("C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\Shortcut.lnk")
set x = Nothing
As you can see, the code is exactly the same. However, it gives me a file not found error:
Script: C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\test.vbs
Line: 3
Char: 1
Error: The system cannot find the file specified.
Code: 80070002
Source: (null)
The file path for the script is exactly the same in the error as the path I have put in the code, but it still gives me an error.
Any help would be appreciated.
Note: My username has been replaced with ***** just for the question.
For the shell (.Run, .Exec) pathes containing spaces need quotes. So replace
x.Run("C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\Shortcut.lnk")
with
x.Run """C:\Users\*****\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Test\Shortcut.lnk"""
(cf here and here)

How can I create a file on my desktop from a Ruby script?

I'm building a webcrawler and I want it to output to a new file that is timestamped. I've completed what I thought would be the more difficult part but I cannot seem to get it to save to the desktop.
Dir.chdir "~/Desktop"
dirname = "scraper_out"
filename = "#{time}"
Dir.mkdir(dirname) unless File.exists?(dirname)
Dir.chdir(dirname)
File.new(filename, "w")
It errors out on the first line
`chdir': No such file or directory # dir_chdir - ~/Desktop
I've read the documentation on FileUtils, File and cannot seem to find where people change into nested directories from the root.
Edit: I don't think FileUtils understands the ~.
~/ is not recognized by Ruby in this context.
Try:
Dir.chdir ENV['HOME']+"/Desktop"
This might help you
Create a file in a specified directory

How to open a file with a given path?

i'm trying to programm a photo seeker. I have an issue when i wanted to open these photo from a list of path.
I put the list of path in a listBox, so i can click on choosen path and it stocks it in a string variable but i get an issue when i wanted to open the file.
I searched and i found 2 topics on stack overflow :
Python os module open file above current directory with relative path
import os.path
import sys
basepath = os.path.dirname(__file__)
filepath = os.path.abspath(os.path.join(basepath, "HelloWord.txt"))
f = open(filepath, "r")
I get an error :
File "C:/Users/jguillot/Desktop/test002.py", line 6, in <module>
f = open(filepath, "r")
TypeError: an integer is required
I don't understand it.
I also look at this topic :
Open file in a relative location in Python
which have the same solution as the first one.
To understand my error i go on : https://docs.python.org/2/library/os.path.html
I did :
import os
from os import path
os.path.exits('C:\Users\jguillot\Desktop\HelloWord.txt')
It returns me False
But if i try :
os.path.exits('C:\Users\jguillot\Desktop')
It returns me True
os.path.exists(path) :
Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.
(From python manual)
I don't understand this error.
Can you explain to me please?

Accessing files within a parent folder and it's sub-folders using Python

I have a folder which has subfolders containing 1000s of DICOM images which I want to read in from IDLE and analyze.
I have used the following code to find file paths:
import sys
print sys.path
I subsequently tried placing my folder which I want to access in these file paths, however I still can not access the files and I get the following error:
>>> fp = open(fp, 'rb')
IOError: [Errno 2] No such file or directory: 'IM-0268-0001.dcm'
I have also tried:
sys.path.insert(0, 'C:/desktop/James_Phantom_CT_Dec_16th/Images')
But this did not work for me either. Help much appreciated, very frustrated.
(using Python 2.7, 64 bit windows OS).
When opening a file, Python does not search the path. You must specify the full path to open:
d = 'C:/desktop/James_Phantom_CT_Dec_16th/Images'
fp = open(d +'IM-0268-0001.dcm',"rb")
Edit: d is the string that will hold the path so that you don't have to re-type it for each file. fp will hold the file object that you will work with. The "rb" is the way you want to open the file:
r - read
w - write with truncate
a - append
r+ - read and write
Also, if working in windows, add "b" to work with binary files. See here.

Resources