12

I'm new to python so forgive me if this sounds simple. I want to join a few variables to produce a path. Like this:

AAAABBBBCCCC\2\2014_04\2014_04_01.csv

Id + '\' + TypeOfMachine + '\' + year + '_' + month + '\' + year + '_' + month + '_' + day + '.csv'

How do I concatenate this? I putted single quotes around underline or backslash, but stackoverflow omits/modifies them.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
  • Seems there are multiple ways to solve this, but I ended up using a modified sugestion from kojiro: r'{}\{}'.format('hi', 'bye') Thanks everyone! – Vini.g.fer Apr 01 '14 at 17:15

6 Answers6

16

A backslash is commonly used to escape special strings. For example:

>>> print "hi\nbye"
hi
bye

Telling Python not to count slashes as special is usually as easy as using a "raw" string, which can be written as a string literal by preceding the string with the letter 'r'.

>>> print r"hi\nbye"
hi\nbye

Even a raw string, however, cannot end with an odd number of backslashes. This makes string concatenation tough.

>>> print "hi" + r"\" + "bye"
File "<stdin>", line 1
print "hi" + r"\" + "bye"
                       ^
SyntaxError: invalid syntax

There are several ways to work around this. The easiest is using string formatting:

>>> print r'{}\{}'.format('hi', 'bye')
hi\bye

Another way is to use a double-backslash in a regular string to escape the second backslash with the first:

>>> print 'hi' + '\\' + 'bye'
hi\bye

But all of this assumes you're facing a legitimate need to use backslashes. If all you're trying to do is construct Windows path expressions, just use os.path.join.

kojiro
  • 74,557
  • 19
  • 143
  • 201
12

You should use os.path.join to construct the path.

e.g:

import os
path = os.path.join(Id, TypeOfMachine, year + '_' + month, year + '_' + month + '_' + day + '.csv')

or if you insist on using backslashes, you need to escape them: as, so '\\'

Sohan Jain
  • 2,318
  • 1
  • 16
  • 17
5

Normally, you'd double the backslash:

'\\'

Use os.path.join() to join directory and filename elements, and use string formatting for the rest:

os.path.join(Id, TypeOfMachine, '{}_{}'.format(year, month), 
             '{}_{}_{}.csv'.format(year, month, day))

and let Python take care of using the correct directory separator for your platform for you. This has the advantage that your code becomes portable; it'll work on an OS other than Windows as well.

By using string formatting, you also take care of any non-string arguments; if year, month and day are integers, for example.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • +1 `format` here takes care of converting your datatypes. You cannot use `os.path.join` on `int`s for example. – quornian Apr 01 '14 at 17:00
2

You can simply call the character by its ASCII code. (I'm using Python 3.7).

Example:

In this case, the ASCII code is 92, you can use Python's chr() function to call the character

enter image description here

In this website you can find a list of ASCII codes for more printable characters.

The code used above:

delimiter = chr(92)

FileName = 'Id' + delimiter + 'TypeOfMachine' + delimiter + 'year' + '_' + 'month' + delimiter + 'year' + '_' + 'month' + '_' + 'day' + '.csv'

print(FileName)

Hope it helps.

Ferd
  • 1,273
  • 14
  • 16
1

Without importing os.path module you could simply do:

 my_path = '\\'.join([Id,TypeOfMachine, year + '_' + month, year + '_' + month + '_' + day + '.csv'])
sergpolly
  • 112
  • 8
0

You can also use normal strings like:

Id + '/' + TypeOfMachine + '/' + year + '_' + month + '/' + year + '_' + month + '_' + day + '.csv'
D.Stang
  • 23
  • 5