8

Python コンパイラでパスを 2 行に分割するのに問題があります。これは単にコンパイラ画面上の長いパスであり、ウィンドウを広げすぎる必要があります。print("string") を、open(path) ではなく、正しくコンパイルされる 2 行のコードに分割する方法を知っています。これを書いていると、テキスト ボックスがすべてを 1 行に収めることさえできないことに気付きました。印刷()

`raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles\test          code\rawstringfiles.txt', 'a')`
4

4 に答える 4

7

Python では、文字列を隣接して配置するだけで文字列を連結できます。

In [67]: 'abc''def'
Out[67]: 'abcdef'

In [68]: r'abc'r'def'
Out[68]: 'abcdef'

In [69]: (r'abc'
   ....: r'def')
Out[69]: 'abcdef'

したがって、このようなものがうまくいくはずです。

raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles'
                      r'\testcode\rawstringfiles.txt', 'a')

別のオプションは、次を使用することos.path.joinです。

myPath = os.path.join(r'C:\Users\Public\Documents\year 2013\testfiles',
                      r'testcode\rawstringfiles.txt')
raw_StringFile = open(myPath, 'a')
于 2013-10-01T20:13:56.327 に答える
1

Python にはImplicit line Joinと呼ばれる気の利いた機能があります。

括弧、角括弧、または中括弧内の式は、バックスラッシュを使用せずに複数の物理行に分割できます。例えば:

month_names = ['Januari', 'Februari', 'Maart',      # These are the
               'April',   'Mei',      'Juni',       # Dutch names
               'Juli',    'Augustus', 'September',  # for the months
               'Oktober', 'November', 'December']   # of the year

だからあなたの質問のために -

raw_StringFile = open(r'C:\Users\Public\Documents\year 2013\testfiles'
                      r'\testcode\rawstringfiles.txt', 'a')

編集- この例では、実際には文字列リテラル連結です。

于 2013-10-01T20:20:01.337 に答える