Created by Nathan Kelber and Zhuo Chen for JSTOR Labs under Creative Commons CC BY License


Python Intermediate 3#

Description: This notebook describes how to:

  • Use the Python pathLib library to manipulate files

Knowledge Required:

Knowledge Recommended:


# Import pathlib library
from pathlib import Path

# Import a tokenizer
from nltk.tokenize import TreebankWordTokenizer

# Import a Counter Objects for counting words
from collections import Counter

# Load the NLTK stopwords list
from nltk.corpus import stopwords
stop_words = stopwords.words('english')

# Import the CSV module
import csv

# Import libraries for making wordclouds
from wordcloud import WordCloud
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from PIL import Image
### Download Sample Files for this Lesson
import urllib.request
from pathlib import Path

# Check if a data folder exists. If not, create it.
data_folder = Path('../data/')
data_folder.mkdir(exist_ok=True)

download_urls = [
    'https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/sample.txt',
    'https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/books.zip',
    'https://ithaka-labs.s3.amazonaws.com/static-files/images/tdm/tdmdocs/sample_cloud.png'
]

for url in download_urls:
    urllib.request.urlretrieve(url, '../data/' + url.rsplit('/', 1)[-1])

import zipfile
with zipfile.ZipFile("../data/books.zip", "r") as zip_ref:
    zip_ref.extractall("../data/books")

bookzip_path = Path.cwd() / '..' / 'data' / 'books.zip'
bookzip_path.unlink()

print('Sample files ready.')
Sample files ready.

A tree structure of filesystem#

An Introduction to pathlib#

Python Intermediate 2 describes the way to open, read, and write files. The built-in module pathlib is the best way to connect to the file system so your code can work seamlessly with files and directories, even across different operating systems. For example, pathlib can help you accomplish tasks like:

  • Find whether a particular file or directory already exists

  • Simplify the code for reading and writing to files

  • Find information about a file, including its extension

  • Iterate a process over a large group of files in a directory (including subdirectories)

The pathlib module improves on os:#

Python also includes an older module named os for working with filepaths. The os module is not deprecated, but most Python programmers will tell you to move to the newer module. The os module treats paths as strings which leads to a lot of issues, especially when working with different operating systems. pathlib has a much nicer syntax for things like joining paths, creating directories, stemming paths, finding parents, and iterating over files. Ultimately, many of the same tasks can be done with os, but the code is often longer and the syntax tends to be more difficult to read and write. If you are using someone else’s code that contains os in it, it is worth learning os to understand the code. For most practical purposes, however, pathlib is superior and it is a good idea to write new code using pathlib instead of os.

Finding and Defining Paths#

We can find the current working directory by using the .cwd() method. (This is similar to the unix command pwd.)

# Get the current working directory
Path.cwd()

Since Constellate runs on a linux server, we will get an path object that is a PosixPath. The directories will end in forward slashes /, whereas on a windows machine a path will use backward slashes, e.g. C:\Windows\.

We can create a path object at any time by using an assignment statement and passing a string into the Path() function. The path can be absolute, starting at the root of the operating system filesystem, or it can be relative, starting from the current working directory.

# Create a path object based on a string from a relative path
file_path = Path('../data/sample.txt')

We have created a path object, not simply a string. The path object has a lot more flexibility than a traditional string (like what is used in the os module). It allows us to create code that is easier to adapt for different operating systems since we do not have to be concerned about formatting the string with slashes in the correct direction and other technical issues that diverge from one operating system to another.

# We have created a Path object, not simply a string
type(file_path)
pathlib.PosixPath

We can also use the print() function on the path object.

# Print out the path object
print(file_path)
../data/sample.txt

The .resolve() method will take a relative path object and create an abolute path object. The absolute path is the full path from the root of the filesystem. On a Mac or Linux, this is simply /. On a Windows computer, it is usually C:\.

# Getting the full path using .resolve()
# Returns a path object

file_path.resolve()

We can also build a path based on strings separated by slashes. (This is a much more readable way to join paths than the os module.)

# Building another path off the current working directory
# Using the slash notation

file_path = Path.cwd() / '..' / 'data' / 'sample.txt'
print(file_path)

When we create a path object, there is no check to make sure it points to an actual file or directory. We can check if the path exists with the .exists() method.

# Check if the path exists
# Works for files and directories
file_path.exists()
True

Coding Challenge! < / >

Create a new path object new_path that points to a filed called sample.html in the data folder. Confirm the file does not exist using the .exists() method.

# Checking if data/sample.html exists
# Call the new path object new_path

Now, let’s get a better understanding of the relative path and the absolute path. First, let’s create a test.txt file in the current directory and write some text in it.

Let’s open the file and read in the data.

# Open the text file and read the data
with open('test.txt', 'r') as f:
    print(f.read())

Next, let’s delete the test.txt file and create a test.txt file in the parent directory of the current directory constellate-notebooks. If you try to open the file with the same code from the previous code cell, what will happen?

# Open the test file in the parent directory using the same code
with open('test.txt', 'r') as f:
    print(f.read())

One thing you could do is to give the absolute path instead.

# Open the test file using the absolute path
with open('/home/jovyan/test.txt', 'r') as f:
    print(f.read())

Or, you can use the relative path in the following way.

# Open the test file using the relative path
with open('../test.txt', 'r') as f:
    print(f.read())

Checking if a Path Points to an Existing File or Directory#

Method

Effect

.is_file()

Return a Boolean True/False if the path points at an existing file

.is_dir()

Return a Boolean True/False if the path points at an existing directory

# Check whether path is a file
# Returns a Boolean

file_path.is_file()
True
# Check whether path is a directory
# Returns a Boolean

file_path.is_dir()
False

Path Attributes#

A path object also has useful attributes. Unlike the methods above which end in parentheses (), attributes do not use parentheses. (We can think of a method as function, since it ends in (), that additionally takes an object before the dot notation. These functions usually transform or do some kind of action that may need take arguments in the parentheses. An attribute, on the other hand, is more like a property of the object so it does not require parentheses.)

Attribute

Information Returned

.parent

Return a path object for the parent directory

.parents[x]

Return a path object for parents x generations higher

.name

Return a string containing the file name with extension

.stem

Return a string containing the file name without extension

.suffix

Return a string containing the file extension

# Get the parent of the path
# Returns a path object

file_path.parent
# Finding the grandparent of the path using .parent twice
# Returns a path object
# If the path was specified from a relative path, 
# you may need to use .resolve() to get the absolute path first

file_path.parent.parent
# Getting even deeper into the path
# Finding the grandparent of the full path using parents with index
# Returns a path object

file_path.parents[4] # Try changing the index
# Return just the name of the file or folder
# Returns a string
file_path.name
'sample.txt'
# Return just the name of the file without extension
# Returns a string

file_path.stem
'sample'
# Return just the extension/suffix
# Returns a string

file_path.suffix
'.txt'

Creating Files and Directories#

To create a new file or directory, first create the desired path object then use the appropriate method:

  • .touch() will create a new file

  • .mkdir() will create a new directory

# Create a new file

new_file_path = Path.cwd() / '..' /'data' / 'new_file.txt'
new_file_path.touch()
# Create a new directory

new_dir_path = Path.cwd() / '..' /'data' / 'new_directory'

# Create the directory
new_dir_path.mkdir(exist_ok = True) # The exist_ok = True parameter does not raise errors if directory exists

Removing Files and Directories#

To remove a file or directory, first create the path object then use the appropriate method:

  • .unlink() will delete a file

  • .rmdir() will delete a directory

# Remove a file

new_file_path.unlink()
# Remove a directory

new_dir_path.rmdir()

Note: There is no method in Pathlib to recursively delete directories. In that case, you may want to import the module shutil and use the .rmtree() method: shutil.rmtree(path).

Rename a File or Directory#

To rename a file, you will need two path objects: the original path object and a new path object with the new name. The syntax looks like:

old_path.rename(new_path)

# Create an original file for this example

old_path = Path.cwd() / '..' /'data' / 'original_file.txt'
old_path.touch()
# Rename the original file with `.rename()`
# On Windows, if the renamed file already exists an error will occur
# On Unix, if the renamed file already exists the file will be overwritten silently

new_path = Path.cwd() / '..' /'data' / 'renamed_file.txt'
old_path.rename(new_path)

Open, Read, and Write to Text Files#

Path objects work with the context manager with open. Instead of passing a string into the open() function, we can add the path object onto the front of a .open() method.

# Opening the file with a context manager
# and the `.open()` method
# The 'r', read only mode, argument is optional with `.open()`

with file_path.open() as f:
    print(f.read())
A text file can have many words in it
These words are written on the second line
Third line
Fourth line
Fifth line
Sixth line
Seventh line
Eighth line
Ninth line
Tenth line. This is the end of the text file!

Coding Challenge! < / >

In the directory data/books/, there is a copy of Bram Stoker’s Dracula from Project Gutenberg called dracula_bram_stoker.txt. Create a path object pointing to the file. Then open the file and print out the first 5000 characters.

For a tougher challenge, print out the first 500 lines. (The .readlines() method will return a list whose elements are each line of the document.)

# Print out 5000 characters of Bram Stoker's Dracula
# Print out 500 lines of Bram Stoker's Dracula

Quickly Reading or Writing a File#

If you are reading a small text file, then there is an even shorter way to read the file using a path object: .read_text(). This method opens the file, creates a string from the file object contents, and then closes the file object automatically.

# Using the read_text method
# Returns a string
print(file_path.read_text())
A text file can have many words in it
These words are written on the second line
Third line
Fourth line
Fifth line
Sixth line
Seventh line
Eighth line
Ninth line
Tenth line. This is the end of the text file!

There is also a fast method for writing to a file using a path object: .write_text(). This method opens the file object in write mode, writes a string to the file, and then closes it automatically. Be careful with this method since it will overwrite any existing files!

# Create a new file

new_file_path = Path.cwd() / '..' /'data' / 'new_file.txt'

# Write to a file
# This overwrites the file if it already exists

new_file_path.write_text('Hello World!')
print(new_file_path.read_text())
Hello World!

Coding Challenge! < / >

Create a new file and add Shakespeare’s Sonnet VI from the sonnet_string.

For a greater challenge, write a program that will print the line numbers at the beginner of each line.

For an even greater challenge, print out the rhyme scheme at the end of each line: ababcdcdefefgg

# Create a file that contains the sonnet string below
# Open the file and print it out line by line
# At the beginning of each line, print the appropriate line number

sonnet_string = """Then let not winter's ragged hand deface,
In thee thy summer, ere thou be distilled:
Make sweet some vial; treasure thou some place
With beauty's treasure ere it be self-killed.
That use is not forbidden usury,
Which happies those that pay the willing loan;
That's for thy self to breed another thee,
Or ten times happier, be it ten for one;
Ten times thy self were happier than thou art,
If ten of thine ten times refigured thee:
Then what could death do if thou shouldst depart,
Leaving thee living in posterity?
   Be not self-willed, for thou art much too fair
   To be death's conquest and make worms thine heir."""

Gathering a List of Files with .iterdir() and .rglob()#

It is common to gather a list of files in a directory (or set of directories) in order to excecute code on each one at a time. If all the files are in a single directory, then

# Use .iterdir() to iterate over files in a directory

input_dir = Path.cwd() / '..' /'data' / 'books'
for file in input_dir.iterdir():
    print(file)
# Use .iterdir() to iterate over files in a directory
# Checking for a particular extension
# Only works for a single directory!

for file in input_dir.iterdir():
    if file.suffix == '.txt':
        print(file)

The .iterdir() method will work on a single directory, but if you have multiple nested directories then you can use the .rglob() method. Be careful with this method, however, since if there are a lot of nested folders it could take a very long time to process the whole directory tree.

# Use glob to iterate over all files including subfolders

for file in input_dir.rglob("*.txt"):
    print(file)

Examples#

Tokenize a Directory of Texts and Find Most Common Words#

def tokenize_and_count(text):
    """Take in a string and convert into a list of token strings.
    Remove symbol tokens. Remove common tokens using a stop_words list.
    Then convert those token strings into a Counter object."""
    tokens = TreebankWordTokenizer().tokenize(text)
    cleaned_tokens = []
    for token in tokens:
        token = token.lower()
        if token in stop_words:
            continue
        if not token.isalpha():
            continue
        cleaned_tokens.append(token)
    counts = Counter(cleaned_tokens)
    return counts
# Print out the 20 most common words in each text

for file in input_dir.iterdir():
    if file.suffix == '.txt':
         with file.open() as f:
                contents = f.read()
                counts = tokenize_and_count(contents)
                most_common = counts.most_common(20)
                print(file.name)
                for (token, count) in most_common:
                    print(token.ljust(15), count)
alice_in_wonderland_lewis_carroll.txt
said            453
alice           331
little          126
one             95
project         88
like            83
went            81
would           73
thought         72
could           71
see             67
know            65
time            64
mock            57
queen           57
began           54
quite           53
must            51
much            50
way             50
a_room_with_a_view_e_m_forster.txt
said            418
miss            408
lucy            360
would           266
one             222
cecil           194
beebe           173
bartlett        168
honeychurch     134
must            134
go              134
like            132
could           130
never           128
know            126
little          117
george          115
say             104
see             102
man             102
dracula_bram_stoker.txt
said            532
could           497
one             473
must            448
would           446
shall           425
may             404
us              394
see             387
know            364
time            332
van             323
come            318
came            307
went            297
helsing         296
like            276
go              274
lucy            265
even            244
adventures_of_sherlock_holmes_a_conan_doyle.txt
said            470
upon            461
holmes          357
one             337
would           324
could           286
little          267
man             259
see             219
may             207
must            171
shall           171
think           167
know            157
us              150
came            144
come            143
two             138
time            135
might           125
moby_dick_herman_melville.txt
one             863
whale           862
like            570
upon            560
old             440
would           426
ye              401
man             397
ahab            390
though          357
ship            347
yet             329
sea             310
chapter         305
long            305
still           305
great           304
said            296
must            291
time            286
pride_and_prejudice_jane_austen.txt
elizabeth       562
could           515
would           462
said            399
darcy           345
must            313
much            308
bennet          280
miss            272
one             256
jane            246
bingley         233
know            226
though          216
never           215
soon            206
think           203
may             199
might           199
every           197
little_women_louisa_m_alcott.txt
jo              1335
one             867
said            829
little          770
meg             648
amy             633
laurie          581
like            569
would           528
beth            454
could           425
see             402
good            400
go              377
old             375
never           373
much            355
mother          351
well            321
away            308
middlemarch_george_eliot.txt
said            2201
would           1513
could           831
lydgate         790
one             769
dorothea        717
might           646
like            642
little          599
man             579
must            574
know            553
never           533
think           516
much            515
made            503
good            497
see             481
say             472
casaubon        469

Word Clouds for a Directory of Text Files#

# Define how a wordcloud should look

def make_cloud(counts):
    # Adding a mask shape of a cloud to your word cloud
    # By default, the shape will be a rectangle
    # You can specify any shape you like based on an image file
    cloud_mask = np.array(Image.open('../data/sample_cloud.png')) # Specifies the location of the mask shape
    cloud_mask = np.where(cloud_mask > 3, 255, cloud_mask) # this line will take all values greater than 3 and make them 255 (white)
    
    ### Specify word cloud details
    wordcloud = WordCloud(
        width = 800, # Change the pixel width of the image if blurry
        height = 600, # Change the pixel height of the image if blurry
        background_color = "white", # Change the background color
        colormap = 'viridis', # The colors of the words, see https://matplotlib.org/stable/tutorials/colors/colormaps.html
        max_words = 150, # Change the max number of words shown
        min_font_size = 4, # Do not show small text
    
    # Add a shape and outline (known as a mask) to your wordcloud
        contour_color = 'blue', # The outline color of your mask shape
        mask = cloud_mask, # 
        contour_width = 1
    ).generate_from_frequencies(counts)

    mpl.rcParams['figure.figsize'] = (20,20) # Change the image size displayed
    plt.imshow(wordcloud, interpolation='bilinear')
    plt.axis("off")
    plt.show()
# Create a single wordcloud

text_path = Path.cwd() / '..' /'data' / 'books' / 'alice_in_wonderland_lewis_carroll.txt'
with text_path.open() as f:
    contents = f.read()
    counts = tokenize_and_count(contents)
    make_cloud(counts)
../../../../_images/5388f87d8618e899b37739943190e8224181c8fc4869ba7bff97781498650d2d.png
# Create wordclouds for every text in the directory

for file in input_dir.iterdir():
    if file.suffix == '.txt':
         with file.open() as f:
                contents = f.read()
                counts = tokenize_and_count(contents)
                make_cloud(counts)
../../../../_images/99c90404f01c7dad3e0805b16e968f40005976030d3cd021ced20c1a04a85737.png ../../../../_images/320fafafb60a45d14b16a2096046bd16c98af99962212d9a4cf2cc2a87445d7c.png ../../../../_images/29b9c37e6d825645999eacdecc65edb2617b55ed606ee25780a8199ae130c55f.png ../../../../_images/9132eef0c4176445cba650665e05a212703d68e1df9c35dbafc6ba88804cdf0f.png ../../../../_images/31f2f7f911d8b1a42477f21c89a6cec63c44446e270cf40d3985fe6015974fe8.png ../../../../_images/59c2bc20599680d3e8be6dcfce497c48b48bff4ebd0160a38ad620f080a21f44.png ../../../../_images/9f237e8542cebdeb51ce8e6fa396857f7492c146606bad80a427ec4215e84f7b.png ../../../../_images/d3078702ea5834ac6af0e57ecb0910047e9c34f2fb5c5990df4b6c9f4059e113.png

Lesson Complete#

Congratulations! You have completed Python Intermediate 3.

Start Next Lesson: Python Intermediate 4#

Exercise Solutions#

Here are a few solutions for exercises in this lesson.

# Checking if data/sample.html exists
new_path = Path.cwd() / '..' /'data' / 'sample.html'
new_path.exists()
False
# Print out 5000 characters of Bram Stoker's Dracula
dracula_path = Path.cwd() / '..' /'data' / 'books' / 'dracula_bram_stoker.txt'

with dracula_path.open() as f:
    contents = f.read()
    print(contents[:5000])
The Project Gutenberg eBook of Dracula, by Bram Stoker

This eBook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United States, you
will have to check the laws of the country where you are located before
using this eBook.

Title: Dracula

Author: Bram Stoker

Release Date: October, 1995 [eBook #345]
[Most recently updated: October 5, 2022]

Language: English


Produced by: Chuck Greif and the Online Distributed Proofreading Team

*** START OF THE PROJECT GUTENBERG EBOOK DRACULA ***




                                DRACULA

                                  _by_

                              Bram Stoker

                        [Illustration: colophon]

                                NEW YORK

                            GROSSET & DUNLAP

                              _Publishers_

      Copyright, 1897, in the United States of America, according
                   to Act of Congress, by Bram Stoker

                        [_All rights reserved._]

                      PRINTED IN THE UNITED STATES
                                   AT
               THE COUNTRY LIFE PRESS, GARDEN CITY, N.Y.




                                   TO

                             MY DEAR FRIEND

                               HOMMY-BEG




Contents

CHAPTER I. Jonathan Harker’s Journal
CHAPTER II. Jonathan Harker’s Journal
CHAPTER III. Jonathan Harker’s Journal
CHAPTER IV. Jonathan Harker’s Journal
CHAPTER V. Letters—Lucy and Mina
CHAPTER VI. Mina Murray’s Journal
CHAPTER VII. Cutting from “The Dailygraph,” 8 August
CHAPTER VIII. Mina Murray’s Journal
CHAPTER IX. Mina Murray’s Journal
CHAPTER X. Mina Murray’s Journal
CHAPTER XI. Lucy Westenra’s Diary
CHAPTER XII. Dr. Seward’s Diary
CHAPTER XIII. Dr. Seward’s Diary
CHAPTER XIV. Mina Harker’s Journal
CHAPTER XV. Dr. Seward’s Diary
CHAPTER XVI. Dr. Seward’s Diary
CHAPTER XVII. Dr. Seward’s Diary
CHAPTER XVIII. Dr. Seward’s Diary
CHAPTER XIX. Jonathan Harker’s Journal
CHAPTER XX. Jonathan Harker’s Journal
CHAPTER XXI. Dr. Seward’s Diary
CHAPTER XXII. Jonathan Harker’s Journal
CHAPTER XXIII. Dr. Seward’s Diary
CHAPTER XXIV. Dr. Seward’s Phonograph Diary, spoken by Van Helsing
CHAPTER XXV. Dr. Seward’s Diary
CHAPTER XXVI. Dr. Seward’s Diary
CHAPTER XXVII. Mina Harker’s Journal




DRACULA




CHAPTER I

JONATHAN HARKER'S JOURNAL

(_Kept in shorthand._)


_3 May. Bistritz._--Left Munich at 8:35 P. M., on 1st May, arriving at
Vienna early next morning; should have arrived at 6:46, but train was an
hour late. Buda-Pesth seems a wonderful place, from the glimpse which I
got of it from the train and the little I could walk through the
streets. I feared to go very far from the station, as we had arrived
late and would start as near the correct time as possible. The
impression I had was that we were leaving the West and entering the
East; the most western of splendid bridges over the Danube, which is
here of noble width and depth, took us among the traditions of Turkish
rule.

We left in pretty good time, and came after nightfall to Klausenburgh.
Here I stopped for the night at the Hotel Royale. I had for dinner, or
rather supper, a chicken done up some way with red pepper, which was
very good but thirsty. (_Mem._, get recipe for Mina.) I asked the
waiter, and he said it was called "paprika hendl," and that, as it was a
national dish, I should be able to get it anywhere along the
Carpathians. I found my smattering of German very useful here; indeed, I
don't know how I should be able to get on without it.

Having had some time at my disposal when in London, I had visited the
British Museum, and made search among the books and maps in the library
regarding Transylvania; it had struck me that some foreknowledge of the
country could hardly fail to have some importance in dealing with a
nobleman of that country. I find that the district he named is in the
extreme east of the country, just on the borders of three states,
Transylvania, Moldavia and Bukovina, in the midst of the Carpathian
mountains; one of the wildest and least known portions of Europe. I was
not able to light on any map or work giving the exact locality of the
Castle Dracula, as there are no maps of this country as yet to compare
with our own Ordnance Survey maps; but I found that Bistritz, the post
town named by Count Dracula, is a fairly well-known place. I shall enter
here some of my notes, as they may refresh my memory when I talk over my
travels with Mina.

In the population of Transylvania there are four distinct nationalities:
Saxons in the South, and mixed with them the Wallachs, who are the
descendants of the Dacians; Magyars in the West, and Szekelys in the
East and North. I am going among the latter, who claim to be descended
from Attila 
# Print out 500 lines of Bram Stoker's Dracula

dracula_path = Path.cwd() / '..' /'data' / 'books' / 'dracula_bram_stoker.txt'

with dracula_path.open() as f:
    contents = f.readlines()
    shortened = contents[:500]
    for line in shortened:
        print(line, end='')
The Project Gutenberg eBook of Dracula, by Bram Stoker

This eBook is for the use of anyone anywhere in the United States and
most other parts of the world at no cost and with almost no restrictions
whatsoever. You may copy it, give it away or re-use it under the terms
of the Project Gutenberg License included with this eBook or online at
www.gutenberg.org. If you are not located in the United States, you
will have to check the laws of the country where you are located before
using this eBook.

Title: Dracula

Author: Bram Stoker

Release Date: October, 1995 [eBook #345]
[Most recently updated: October 5, 2022]

Language: English


Produced by: Chuck Greif and the Online Distributed Proofreading Team

*** START OF THE PROJECT GUTENBERG EBOOK DRACULA ***




                                DRACULA

                                  _by_

                              Bram Stoker

                        [Illustration: colophon]

                                NEW YORK

                            GROSSET & DUNLAP

                              _Publishers_

      Copyright, 1897, in the United States of America, according
                   to Act of Congress, by Bram Stoker

                        [_All rights reserved._]

                      PRINTED IN THE UNITED STATES
                                   AT
               THE COUNTRY LIFE PRESS, GARDEN CITY, N.Y.




                                   TO

                             MY DEAR FRIEND

                               HOMMY-BEG




Contents

CHAPTER I. Jonathan Harker’s Journal
CHAPTER II. Jonathan Harker’s Journal
CHAPTER III. Jonathan Harker’s Journal
CHAPTER IV. Jonathan Harker’s Journal
CHAPTER V. Letters—Lucy and Mina
CHAPTER VI. Mina Murray’s Journal
CHAPTER VII. Cutting from “The Dailygraph,” 8 August
CHAPTER VIII. Mina Murray’s Journal
CHAPTER IX. Mina Murray’s Journal
CHAPTER X. Mina Murray’s Journal
CHAPTER XI. Lucy Westenra’s Diary
CHAPTER XII. Dr. Seward’s Diary
CHAPTER XIII. Dr. Seward’s Diary
CHAPTER XIV. Mina Harker’s Journal
CHAPTER XV. Dr. Seward’s Diary
CHAPTER XVI. Dr. Seward’s Diary
CHAPTER XVII. Dr. Seward’s Diary
CHAPTER XVIII. Dr. Seward’s Diary
CHAPTER XIX. Jonathan Harker’s Journal
CHAPTER XX. Jonathan Harker’s Journal
CHAPTER XXI. Dr. Seward’s Diary
CHAPTER XXII. Jonathan Harker’s Journal
CHAPTER XXIII. Dr. Seward’s Diary
CHAPTER XXIV. Dr. Seward’s Phonograph Diary, spoken by Van Helsing
CHAPTER XXV. Dr. Seward’s Diary
CHAPTER XXVI. Dr. Seward’s Diary
CHAPTER XXVII. Mina Harker’s Journal




DRACULA




CHAPTER I

JONATHAN HARKER'S JOURNAL

(_Kept in shorthand._)


_3 May. Bistritz._--Left Munich at 8:35 P. M., on 1st May, arriving at
Vienna early next morning; should have arrived at 6:46, but train was an
hour late. Buda-Pesth seems a wonderful place, from the glimpse which I
got of it from the train and the little I could walk through the
streets. I feared to go very far from the station, as we had arrived
late and would start as near the correct time as possible. The
impression I had was that we were leaving the West and entering the
East; the most western of splendid bridges over the Danube, which is
here of noble width and depth, took us among the traditions of Turkish
rule.

We left in pretty good time, and came after nightfall to Klausenburgh.
Here I stopped for the night at the Hotel Royale. I had for dinner, or
rather supper, a chicken done up some way with red pepper, which was
very good but thirsty. (_Mem._, get recipe for Mina.) I asked the
waiter, and he said it was called "paprika hendl," and that, as it was a
national dish, I should be able to get it anywhere along the
Carpathians. I found my smattering of German very useful here; indeed, I
don't know how I should be able to get on without it.

Having had some time at my disposal when in London, I had visited the
British Museum, and made search among the books and maps in the library
regarding Transylvania; it had struck me that some foreknowledge of the
country could hardly fail to have some importance in dealing with a
nobleman of that country. I find that the district he named is in the
extreme east of the country, just on the borders of three states,
Transylvania, Moldavia and Bukovina, in the midst of the Carpathian
mountains; one of the wildest and least known portions of Europe. I was
not able to light on any map or work giving the exact locality of the
Castle Dracula, as there are no maps of this country as yet to compare
with our own Ordnance Survey maps; but I found that Bistritz, the post
town named by Count Dracula, is a fairly well-known place. I shall enter
here some of my notes, as they may refresh my memory when I talk over my
travels with Mina.

In the population of Transylvania there are four distinct nationalities:
Saxons in the South, and mixed with them the Wallachs, who are the
descendants of the Dacians; Magyars in the West, and Szekelys in the
East and North. I am going among the latter, who claim to be descended
from Attila and the Huns. This may be so, for when the Magyars conquered
the country in the eleventh century they found the Huns settled in it. I
read that every known superstition in the world is gathered into the
horseshoe of the Carpathians, as if it were the centre of some sort of
imaginative whirlpool; if so my stay may be very interesting. (_Mem._, I
must ask the Count all about them.)

I did not sleep well, though my bed was comfortable enough, for I had
all sorts of queer dreams. There was a dog howling all night under my
window, which may have had something to do with it; or it may have been
the paprika, for I had to drink up all the water in my carafe, and was
still thirsty. Towards morning I slept and was wakened by the continuous
knocking at my door, so I guess I must have been sleeping soundly then.
I had for breakfast more paprika, and a sort of porridge of maize flour
which they said was "mamaliga," and egg-plant stuffed with forcemeat, a
very excellent dish, which they call "impletata." (_Mem._, get recipe
for this also.) I had to hurry breakfast, for the train started a little
before eight, or rather it ought to have done so, for after rushing to
the station at 7:30 I had to sit in the carriage for more than an hour
before we began to move. It seems to me that the further east you go the
more unpunctual are the trains. What ought they to be in China?

All day long we seemed to dawdle through a country which was full of
beauty of every kind. Sometimes we saw little towns or castles on the
top of steep hills such as we see in old missals; sometimes we ran by
rivers and streams which seemed from the wide stony margin on each side
of them to be subject to great floods. It takes a lot of water, and
running strong, to sweep the outside edge of a river clear. At every
station there were groups of people, sometimes crowds, and in all sorts
of attire. Some of them were just like the peasants at home or those I
saw coming through France and Germany, with short jackets and round hats
and home-made trousers; but others were very picturesque. The women
looked pretty, except when you got near them, but they were very clumsy
about the waist. They had all full white sleeves of some kind or other,
and most of them had big belts with a lot of strips of something
fluttering from them like the dresses in a ballet, but of course there
were petticoats under them. The strangest figures we saw were the
Slovaks, who were more barbarian than the rest, with their big cow-boy
hats, great baggy dirty-white trousers, white linen shirts, and enormous
heavy leather belts, nearly a foot wide, all studded over with brass
nails. They wore high boots, with their trousers tucked into them, and
had long black hair and heavy black moustaches. They are very
picturesque, but do not look prepossessing. On the stage they would be
set down at once as some old Oriental band of brigands. They are,
however, I am told, very harmless and rather wanting in natural
self-assertion.

It was on the dark side of twilight when we got to Bistritz, which is a
very interesting old place. Being practically on the frontier--for the
Borgo Pass leads from it into Bukovina--it has had a very stormy
existence, and it certainly shows marks of it. Fifty years ago a series
of great fires took place, which made terrible havoc on five separate
occasions. At the very beginning of the seventeenth century it underwent
a siege of three weeks and lost 13,000 people, the casualties of war
proper being assisted by famine and disease.

Count Dracula had directed me to go to the Golden Krone Hotel, which I
found, to my great delight, to be thoroughly old-fashioned, for of
course I wanted to see all I could of the ways of the country. I was
evidently expected, for when I got near the door I faced a
cheery-looking elderly woman in the usual peasant dress--white
undergarment with long double apron, front, and back, of coloured stuff
fitting almost too tight for modesty. When I came close she bowed and
said, "The Herr Englishman?" "Yes," I said, "Jonathan Harker." She
smiled, and gave some message to an elderly man in white shirt-sleeves,
who had followed her to the door. He went, but immediately returned with
a letter:--

     "My Friend.--Welcome to the Carpathians. I am anxiously expecting
     you. Sleep well to-night. At three to-morrow the diligence will
     start for Bukovina; a place on it is kept for you. At the Borgo
     Pass my carriage will await you and will bring you to me. I trust
     that your journey from London has been a happy one, and that you
     will enjoy your stay in my beautiful land.

"Your friend,

"DRACULA."


_4 May._--I found that my landlord had got a letter from the Count,
directing him to secure the best place on the coach for me; but on
making inquiries as to details he seemed somewhat reticent, and
pretended that he could not understand my German. This could not be
true, because up to then he had understood it perfectly; at least, he
answered my questions exactly as if he did. He and his wife, the old
lady who had received me, looked at each other in a frightened sort of
way. He mumbled out that the money had been sent in a letter, and that
was all he knew. When I asked him if he knew Count Dracula, and could
tell me anything of his castle, both he and his wife crossed themselves,
and, saying that they knew nothing at all, simply refused to speak
further. It was so near the time of starting that I had no time to ask
any one else, for it was all very mysterious and not by any means
comforting.

Just before I was leaving, the old lady came up to my room and said in a
very hysterical way:

"Must you go? Oh! young Herr, must you go?" She was in such an excited
state that she seemed to have lost her grip of what German she knew, and
mixed it all up with some other language which I did not know at all. I
was just able to follow her by asking many questions. When I told her
that I must go at once, and that I was engaged on important business,
she asked again:

"Do you know what day it is?" I answered that it was the fourth of May.
She shook her head as she said again:

"Oh, yes! I know that! I know that, but do you know what day it is?" On
my saying that I did not understand, she went on:

"It is the eve of St. George's Day. Do you not know that to-night, when
the clock strikes midnight, all the evil things in the world will have
full sway? Do you know where you are going, and what you are going to?"
She was in such evident distress that I tried to comfort her, but
without effect. Finally she went down on her knees and implored me not
to go; at least to wait a day or two before starting. It was all very
ridiculous but I did not feel comfortable. However, there was business
to be done, and I could allow nothing to interfere with it. I therefore
tried to raise her up, and said, as gravely as I could, that I thanked
her, but my duty was imperative, and that I must go. She then rose and
dried her eyes, and taking a crucifix from her neck offered it to me. I
did not know what to do, for, as an English Churchman, I have been
taught to regard such things as in some measure idolatrous, and yet it
seemed so ungracious to refuse an old lady meaning so well and in such a
state of mind. She saw, I suppose, the doubt in my face, for she put the
rosary round my neck, and said, "For your mother's sake," and went out
of the room. I am writing up this part of the diary whilst I am waiting
for the coach, which is, of course, late; and the crucifix is still
round my neck. Whether it is the old lady's fear, or the many ghostly
traditions of this place, or the crucifix itself, I do not know, but I
am not feeling nearly as easy in my mind as usual. If this book should
ever reach Mina before I do, let it bring my good-bye. Here comes the
coach!

       *       *       *       *       *

_5 May. The Castle._--The grey of the morning has passed, and the sun is
high over the distant horizon, which seems jagged, whether with trees or
hills I know not, for it is so far off that big things and little are
mixed. I am not sleepy, and, as I am not to be called till I awake,
naturally I write till sleep comes. There are many odd things to put
down, and, lest who reads them may fancy that I dined too well before I
left Bistritz, let me put down my dinner exactly. I dined on what they
called "robber steak"--bits of bacon, onion, and beef, seasoned with red
pepper, and strung on sticks and roasted over the fire, in the simple
style of the London cat's meat! The wine was Golden Mediasch, which
produces a queer sting on the tongue, which is, however, not
disagreeable. I had only a couple of glasses of this, and nothing else.

When I got on the coach the driver had not taken his seat, and I saw him
talking with the landlady. They were evidently talking of me, for every
now and then they looked at me, and some of the people who were sitting
on the bench outside the door--which they call by a name meaning
"word-bearer"--came and listened, and then looked at me, most of them
pityingly. I could hear a lot of words often repeated, queer words, for
there were many nationalities in the crowd; so I quietly got my polyglot
dictionary from my bag and looked them out. I must say they were not
cheering to me, for amongst them were "Ordog"--Satan, "pokol"--hell,
"stregoica"--witch, "vrolok" and "vlkoslak"--both of which mean the same
thing, one being Slovak and the other Servian for something that is
either were-wolf or vampire. (_Mem._, I must ask the Count about these
superstitions)

When we started, the crowd round the inn door, which had by this time
swelled to a considerable size, all made the sign of the cross and
pointed two fingers towards me. With some difficulty I got a
fellow-passenger to tell me what they meant; he would not answer at
first, but on learning that I was English, he explained that it was a
charm or guard against the evil eye. This was not very pleasant for me,
just starting for an unknown place to meet an unknown man; but every one
seemed so kind-hearted, and so sorrowful, and so sympathetic that I
could not but be touched. I shall never forget the last glimpse which I
had of the inn-yard and its crowd of picturesque figures, all crossing
themselves, as they stood round the wide archway, with its background of
rich foliage of oleander and orange trees in green tubs clustered in the
centre of the yard. Then our driver, whose wide linen drawers covered
the whole front of the box-seat--"gotza" they call them--cracked his big
whip over his four small horses, which ran abreast, and we set off on
our journey.

I soon lost sight and recollection of ghostly fears in the beauty of the
scene as we drove along, although had I known the language, or rather
languages, which my fellow-passengers were speaking, I might not have
been able to throw them off so easily. Before us lay a green sloping
land full of forests and woods, with here and there steep hills, crowned
with clumps of trees or with farmhouses, the blank gable end to the
road. There was everywhere a bewildering mass of fruit blossom--apple,
plum, pear, cherry; and as we drove by I could see the green grass under
the trees spangled with the fallen petals. In and out amongst these
green hills of what they call here the "Mittel Land" ran the road,
losing itself as it swept round the grassy curve, or was shut out by the
straggling ends of pine woods, which here and there ran down the
hillsides like tongues of flame. The road was rugged, but still we
seemed to fly over it with a feverish haste. I could not understand then
what the haste meant, but the driver was evidently bent on losing no
time in reaching Borgo Prund. I was told that this road is in summertime
excellent, but that it had not yet been put in order after the winter
snows. In this respect it is different from the general run of roads in
the Carpathians, for it is an old tradition that they are not to be kept
in too good order. Of old the Hospadars would not repair them, lest the
Turk should think that they were preparing to bring in foreign troops,
and so hasten the war which was always really at loading point.

Beyond the green swelling hills of the Mittel Land rose mighty slopes
of forest up to the lofty steeps of the Carpathians themselves. Right
and left of us they towered, with the afternoon sun falling full upon
them and bringing out all the glorious colours of this beautiful range,
deep blue and purple in the shadows of the peaks, green and brown where
grass and rock mingled, and an endless perspective of jagged rock and
pointed crags, till these were themselves lost in the distance, where
the snowy peaks rose grandly. Here and there seemed mighty rifts in the
mountains, through which, as the sun began to sink, we saw now and again
the white gleam of falling water. One of my companions touched my arm as
we swept round the base of a hill and opened up the lofty, snow-covered
peak of a mountain, which seemed, as we wound on our serpentine way, to
be right before us:--

"Look! Isten szek!"--"God's seat!"--and he crossed himself reverently.

As we wound on our endless way, and the sun sank lower and lower behind
us, the shadows of the evening began to creep round us. This was
emphasised by the fact that the snowy mountain-top still held the
sunset, and seemed to glow out with a delicate cool pink. Here and there
we passed Cszeks and Slovaks, all in picturesque attire, but I noticed
that goitre was painfully prevalent. By the roadside were many crosses,
and as we swept by, my companions all crossed themselves. Here and there
was a peasant man or woman kneeling before a shrine, who did not even
turn round as we approached, but seemed in the self-surrender of
devotion to have neither eyes nor ears for the outer world. There were
many things new to me: for instance, hay-ricks in the trees, and here
and there very beautiful masses of weeping birch, their white stems
shining like silver through the delicate green of the leaves. Now and
again we passed a leiter-wagon--the ordinary peasant's cart--with its
long, snake-like vertebra, calculated to suit the inequalities of the
road. On this were sure to be seated quite a group of home-coming
peasants, the Cszeks with their white, and the Slovaks with their
coloured, sheepskins, the latter carrying lance-fashion their long
staves, with axe at end. As the evening fell it began to get very cold,
and the growing twilight seemed to merge into one dark mistiness the
gloom of the trees, oak, beech, and pine, though in the valleys which
ran deep between the spurs of the hills, as we ascended through the
Pass, the dark firs stood out here and there against the background of
late-lying snow. Sometimes, as the road was cut through the pine woods
that seemed in the darkness to be closing down upon us, great masses of
greyness, which here and there bestrewed the trees, produced a
peculiarly weird and solemn effect, which carried on the thoughts and
grim fancies engendered earlier in the evening, when the falling sunset
threw into strange relief the ghost-like clouds which amongst the
Carpathians seem to wind ceaselessly through the valleys. Sometimes the
hills were so steep that, despite our driver's haste, the horses could
only go slowly. I wished to get down and walk up them, as we do at home,
but the driver would not hear of it. "No, no," he said; "you must not
walk here; the dogs are too fierce"; and then he added, with what he
evidently meant for grim pleasantry--for he looked round to catch the
approving smile of the rest--"and you may have enough of such matters
before you go to sleep." The only stop he would make was a moment's
pause to light his lamps.

When it grew dark there seemed to be some excitement amongst the
passengers, and they kept speaking to him, one after the other, as
though urging him to further speed. He lashed the horses unmercifully
with his long whip, and with wild cries of encouragement urged them on
to further exertions. Then through the darkness I could see a sort of
patch of grey light ahead of us, as though there were a cleft in the
hills. The excitement of the passengers grew greater; the crazy coach
rocked on its great leather springs, and swayed like a boat tossed on a
stormy sea. I had to hold on. The road grew more level, and we appeared
to fly along. Then the mountains seemed to come nearer to us on each
side and to frown down upon us; we were entering on the Borgo Pass. One
by one several of the passengers offered me gifts, which they pressed
upon me with an earnestness which would take no denial; these were
certainly of an odd and varied kind, but each was given in simple good
faith, with a kindly word, and a blessing, and that strange mixture of
fear-meaning movements which I had seen outside the hotel at
Bistritz--the sign of the cross and the guard against the evil eye.
Then, as we flew along, the driver leaned forward, and on each side the
passengers, craning over the edge of the coach, peered eagerly into the
darkness. It was evident that something very exciting was either
happening or expected, but though I asked each passenger, no one would
give me the slightest explanation. This state of excitement kept on for
some little time; and at last we saw before us the Pass opening out on
the eastern side. There were dark, rolling clouds overhead, and in the
air the heavy, oppressive sense of thunder. It seemed as though the
mountain range had separated two atmospheres, and that now we had got
into the thunderous one. I was now myself looking out for the conveyance
which was to take me to the Count. Each moment I expected to see the
glare of lamps through the blackness; but all was dark. The only light
was the flickering rays of our own lamps, in which the steam from our
hard-driven horses rose in a white cloud. We could see now the sandy
road lying white before us, but there was on it no sign of a vehicle.
The passengers drew back with a sigh of gladness, which seemed to mock
my own disappointment. I was already thinking what I had best do, when
the driver, looking at his watch, said to the others something which I
could hardly hear, it was spoken so quietly and in so low a tone; I
thought it was "An hour less than the time." Then turning to me, he said
in German worse than my own:--

"There is no carriage here. The Herr is not expected after all. He will
now come on to Bukovina, and return to-morrow or the next day; better
the next day." Whilst he was speaking the horses began to neigh and
snort and plunge wildly, so that the driver had to hold them up. Then,
amongst a chorus of screams from the peasants and a universal crossing
of themselves, a calèche, with four horses, drove up behind us, overtook
us, and drew up beside the coach. I could see from the flash of our
lamps, as the rays fell on them, that the horses were coal-black and
splendid animals. They were driven by a tall man, with a long brown
beard and a great black hat, which seemed to hide his face from us. I
could only see the gleam of a pair of very bright eyes, which seemed red
in the lamplight, as he turned to us. He said to the driver:--

"You are early to-night, my friend." The man stammered in reply:--

"The English Herr was in a hurry," to which the stranger replied:--

"That is why, I suppose, you wished him to go on to Bukovina. You cannot
deceive me, my friend; I know too much, and my horses are swift." As he
spoke he smiled, and the lamplight fell on a hard-looking mouth, with
very red lips and sharp-looking teeth, as white as ivory. One of my
companions whispered to another the line from Burger's "Lenore":--

    "Denn die Todten reiten schnell"--
    ("For the dead travel fast.")

The strange driver evidently heard the words, for he looked up with a
gleaming smile. The passenger turned his face away, at the same time
putting out his two fingers and crossing himself. "Give me the Herr's
luggage," said the driver; and with exceeding alacrity my bags were
handed out and put in the calèche. Then I descended from the side of the
coach, as the calèche was close alongside, the driver helping me with a
hand which caught my arm in a grip of steel; his strength must have been
prodigious. Without a word he shook his reins, the horses turned, and we
swept into the darkness of the Pass. As I looked back I saw the steam
from the horses of the coach by the light of the lamps, and projected
against it the figures of my late companions crossing themselves. Then
the driver cracked his whip and called to his horses, and off they swept
on their way to Bukovina. As they sank into the darkness I felt a
strange chill, and a lonely feeling came over me; but a cloak was thrown
over my shoulders, and a rug across my knees, and the driver said in
excellent German:--

"The night is chill, mein Herr, and my master the Count bade me take all
care of you. There is a flask of slivovitz (the plum brandy of the
country) underneath the seat, if you should require it." I did not take
any, but it was a comfort to know it was there all the same. I felt a
little strangely, and not a little frightened. I think had there been
any alternative I should have taken it, instead of prosecuting that
unknown night journey. The carriage went at a hard pace straight along,
then we made a complete turn and went along another straight road. It
seemed to me that we were simply going over and over the same ground
again; and so I took note of some salient point, and found that this was
so. I would have liked to have asked the driver what this all meant, but
I really feared to do so, for I thought that, placed as I was, any
protest would have had no effect in case there had been an intention to
# Create a file that contains the sonnet string below
# Open the file and print it out line by line
# At the beginning of each line, print the appropriate line number

sonnet_string = """Then let not winter's ragged hand deface,
In thee thy summer, ere thou be distilled:
Make sweet some vial; treasure thou some place
With beauty's treasure ere it be self-killed.
That use is not forbidden usury,
Which happies those that pay the willing loan;
That's for thy self to breed another thee,
Or ten times happier, be it ten for one;
Ten times thy self were happier than thou art,
If ten of thine ten times refigured thee:
Then what could death do if thou shouldst depart,
Leaving thee living in posterity?
   Be not self-willed, for thou art much too fair
   To be death's conquest and make worms thine heir."""

# Create the file
new_file = Path.cwd() / '..' /'data' / 'sonnet.txt'
new_file.write_text(sonnet_string)

# Keeping track of lines and rhymes
i = 0
rhyme = 'ababcdcdefefgg'

# Read and print the sonnet line by line

with new_file.open() as f:
    for line in f:
        print(i+1, line[:-1], rhyme[i]) # The final character in each line is a linebreak so we slice it off
        i += 1
1 Then let not winter's ragged hand deface, a
2 In thee thy summer, ere thou be distilled: b
3 Make sweet some vial; treasure thou some place a
4 With beauty's treasure ere it be self-killed. b
5 That use is not forbidden usury, c
6 Which happies those that pay the willing loan; d
7 That's for thy self to breed another thee, c
8 Or ten times happier, be it ten for one; d
9 Ten times thy self were happier than thou art, e
10 If ten of thine ten times refigured thee: f
11 Then what could death do if thou shouldst depart, e
12 Leaving thee living in posterity? f
13    Be not self-willed, for thou art much too fair g
14    To be death's conquest and make worms thine heir g